日历添加事件
EKEventStore* eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
NSDate *startDate = [[NSDate alloc] init];
NSDate *endDate = [[NSDate alloc] init];
event.title = @"New event test";
event.startDate = startDate;
event.endDate = endDate;
event.location = @"浙江";
event.allDay = YES;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
[event addAlarm:[EKAlarm alarmWithRelativeOffset:-30.0f]];
[event addAlarm:[EKAlarm alarmWithRelativeOffset:-15.0f]];
__block NSError *err;
if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
[eventStore requestAccessToEntityType:EKEntityTypeEvent
completion:^(BOOL granted, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (granted) {
[eventStore saveEvent:event span:EKSpanFutureEvents commit:YES error:&err];
} else {
NSLog(@"不允许访问日历");
}
});
}];
} else {
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
}
获取日历中的事件
EKEventStore* eventStore = [[EKEventStore alloc] init];
NSDate* ssdate = [NSDate dateWithTimeIntervalSinceNow:-3600*24*90];
NSDate* ssend = [NSDate dateWithTimeIntervalSinceNow:3600*24*90];
NSPredicate* predicate = [eventStore predicateForEventsWithStartDate:ssdate
endDate:ssend
calendars:nil];
NSArray* events = [eventStore eventsMatchingPredicate:predicate];
for (EKEvent *event in events) {
NSLog(@"=================================================================");
NSLog(@"%@", event.title);
NSLog(@"%@", event.startDate);
NSLog(@"=================================================================");
}