获取日历事件
EKEventStore *eventStore = [[EKEventStore alloc] init];
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc] init];
tempFormatter.dateFormat = @"dd.MM.yyyy HH:mm";
// 获取一个时间段中的日历事件
NSDate *startDate = [tempFormatter dateFromString:@"10.08.2019 15:10"];
NSDate * endDate = [tempFormatter dateFromString:@"12.08.2020 15:30"];
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:@[eventStore.defaultCalendarForNewEvents]];
// eventStore.predicateForEventsWithStartDate(startDate, endDate: endDate, calendars: [eventStore.defaultCalendarForNewEvents])
//获取这个时间段中的所有日程
NSArray*events = [eventStoreeventsMatchingPredicate:predicate];
添加日历事件
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if(error !=nil) {
//发生错误
}elseif(!granted ){
//不允许访问日历
}else{
//创建事件
EKEvent*event = [EKEventeventWithEventStore:eventStore];
event.title=@"lallallallal";
NSDateFormatter*tempFormatter = [[NSDateFormatteralloc]init];
tempFormatter.dateFormat=@"dd.MM. yyyy HH:mm";
//创建一个时间段的日历事件
event.startDate= [tempFormatterdateFromString:@"11.08.2016 15:20"];
event.endDate= [tempFormatterdateFromString:@"11.08.2016 15:30"];
//设置是否为全天事件
event.allDay=false;
//设置事件的提醒时间(相对时间)提前15分钟提醒
[eventaddAlarm:[EKAlarm alarmWithRelativeOffset:-60.0 * 15.0]];
// event.addAlarm(EKAlarm(relativeOffset: -60.0 * 15.0))
//设置事件的提醒时间(绝对时间)
//event.addAlarm(EKAlarm(absoluteDate: NSDate(timeInterval: -60 * 15, sinceDate: event.startDate)))
event.calendar= eventStore.defaultCalendarForNewEvents;
//保存事件,添加到日历中
[eventStoresaveEvent:event span:EKSpanThisEvent commit:YES error:nil];
// [eventStore saveEvent(event, span: .ThisEvent, commit: true)
}
});
}];
查看和编辑日历事件
EKEventViewController *controller = [[EKEventViewController alloc] init];
controller.allowsEditing = YES;//是否可以编辑
controller.event = events.lastObject;//
[self presentViewController:controller animated:YES completion:nil];