ios中将事件同步到系统日历
//获取日历事件
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];
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。