//获取日历事件
EKEventStore* eventStore = [[EKEventStorealloc] init];
NSDate* ssdate = [NSDatedateWithTimeIntervalSinceNow:-3600*24*90];//事件段,开始时间
NSDate* ssend = [NSDatedateWithTimeIntervalSinceNow:3600*24*90];//结束时间,取中间
NSPredicate* predicate = [eventStore predicateForEventsWithStartDate:ssdate
endDate:ssend
calendars:nil];
NSArray* events = [eventStore eventsMatchingPredicate:predicate];//数组里面就是时间段中的EKEvent事件数组
//往日历写事件
EKEvent *event = [EKEventeventWithEventStore: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 addAlarm:[EKAlarmalarmWithRelativeOffset:60.0f * -60.0f * 24]];
[event addAlarm:[EKAlarmalarmWithRelativeOffset:60.0f * -15.0f]];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
__block NSError *err;
//ios 6以后
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];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Event Created"
message:@"Yay!?"
delegate:nil
cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[alert show];
} else {
NSLog(@"不允许访问日历");
}
});
}];
} else {
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
}