iOS10,首先需要满足相关权限:
类型:
typedef NS_ENUM(NSUInteger, EKEntityType) {
EKEntityTypeEvent//事件
EKEntityTypeReminder//提醒
};
具体代码:
{
//事件市场
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error)
{
//错误信息
}
else if (!granted)
{
//被用户拒绝,不允许访问日历
}
else
{
//事件保存到日历
//创建事件
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"哈哈哈,我是提醒事件!";
event.location = @"北三环东路甲5号";
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc] init];
[tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
event.startDate = [[NSDate alloc]init ];
event.endDate = [[NSDate alloc]init ];
event.allDay = YES;
event.notes = @"备注www.baidu.com";
event.URL = [NSURL URLWithString:@"www.baidu.com"];
//添加提醒
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -60.0f * 24]];
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]];
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -5.0f]];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"事件"
message:@"添加成功"
delegate:nil
cancelButtonTitle:@"好的"
otherButtonTitles:nil];
[alert show];
NSLog(@"保存成功");
}
});
}];
}