判断是否可以访问日历
EKEventStore *store = [[EKEventStore alloc] init]; [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { if (granted == YES) { NSLog(@"同意"); }else{ NSLog(@"不同意"); } }];
// 获取适当的日期(Get the appropriate calendar) NSCalendar *calendar = [NSCalendar currentCalendar]; // 创建起始日期组件(Create the start date components) NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init]; oneDayAgoComponents.day = -1; NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents toDate:[NSDate date] options:0]; // 创建结束日期组件(Create the end date components) NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init]; oneYearFromNowComponents.year = 1; NSDate *oneYearFromNow = [calendar dateByAddingComponents:oneYearFromNowComponents toDate:[NSDate date] options:0]; // 用事件库的实例方法创建谓词 (Create the predicate from the event store‘s instance method) NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo endDate:oneYearFromNow calendars:nil]; // 获取所有匹配该谓词的事件(Fetch all events that match the predicate) NSArray *events = [store eventsMatchingPredicate:predicate]; for (EKEvent *e in events) { NSLog(@"%@",e.title); NSLog(@"%@",e.startDate); NSLog(@"%@",e.endDate); NSLog(@"-------------------------"); }
// Get the event store object self.eventStore = [[EKEventStore alloc] init]; // Create a new event EKEvent *event = [EKEvent eventWithEventStore:self.eventStore]; // Create NSDates to hold the start and end date NSDate *startDate = [[NSDate alloc] init]; NSDate *endDate = [[NSDate alloc] init]; // Set properties of the new event object event.title = @"这是测试"; event.startDate = [NSDate dateWithTimeIntervalSinceNow:-60*60]; event.endDate = [NSDate dateWithTimeIntervalSinceNow:-30]; event.allDay = NO; // set event‘s calendar to the default calendar [event setCalendar:[self.eventStore defaultCalendarForNewEvents]]; // Create an NSError pointer NSError *err; // Save the event [self.eventStore saveEvent:event span:EKSpanThisEvent error:&err]; self.idf = [NSString stringWithFormat:@"%@",event.eventIdentifier]; NSLog(@"%@",event.eventIdentifier); // Test for errors if (err == noErr) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Event Created" message:@"How about that?" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil]; [alert show]; }
EKEvent *eventToRemove = [self.eventStore eventWithIdentifier:self.idf]; if ([eventToRemove.eventIdentifier length] > 0) { NSError* error = nil; [self.eventStore removeEvent:eventToRemove span:EKSpanThisEvent error:&error]; if (error) { NSLog(@"%@",error); }else { NSLog(@"ok"); } }
EKEvent *eventToRemove = [self.eventStore eventWithIdentifier:self.idf]; eventToRemove.title = @"已经修改"; // Save the event [self.eventStore saveEvent:eventToRemove span:EKSpanThisEvent error:nil];
参考文章:
http://www.16kan.com/question/detail/265397.html
http://blog.csdn.net/linqir/article/details/8588835
http://supershll.blog.163.com/blog/static/3707043620127284210222/
日历相关EKEvent的学习记录,布布扣,bubuko.com
原文:http://blog.csdn.net/ioswyl88219/article/details/22406451