最重要 添加#import <EventKit/EventKit.h> 库
EKEventStore *eventStore = [[EKEventStore alloc] init];
//请求使用用户的日历数据库
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error){
//错误信息 这里显示错误消息
}else if (!granted){
//被用户拒绝,不允许访问日历 拒绝访问显示错误消息
}else{
// 数据访问权限
// ***** 在这里做重要的事情 *****
//事件保存到日历
//06.07 元素
//title(标题 NSString),
//location(位置NSString),
//startDate(开始时间 2016/06/07 11:14AM),
//endDate(结束时间 2016/06/07 11:14AM),
//addAlarm(提醒时间 2016/06/07 11:14AM),
//notes(备注类容NSString)
//创建事件
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"测试写入日历事件";
event.location = @"北京海淀";
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setAMSymbol:@"AM"];
[dateFormatter setPMSymbol:@"PM"];
[dateFormatter setDateFormat:@"yyyy/MM/dd hh:mmaaa"];
NSDate *date = [NSDate date];
NSString * s = [dateFormatter stringFromDate:date];
DLog(@"%@",s);
//开始时间(必须传)
event.startDate = [date dateByAddingTimeInterval:60 * 2];
//结束时间(必须传)
event.endDate = [date dateByAddingTimeInterval:60 * 5 * 24];
// event.endDate = [[NSDate alloc]init];
// event.allDay = YES;//全天
//添加提醒
//第一次提醒 (几分钟后)
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -1.0f]];
//第二次提醒 ()
// [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -10.0f * 24]];
//06.07 add 事件类容备注
NSString * str = ws.model.remark;
event.notes = [NSString stringWithFormat:@"%@",str];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
DLog(@"保存成功");
//直接杀死进程
exit(2);
}
});
}];
}