EventKit日历事件操作
@property (nonatomic, strong) EKEventStore *eventStore;//设置属性
//添加日程
- (void)addmyEventTitle:(NSString*)title Description:(NSString*)description Time:(NSString*)time {
EKEventStore*eventStore = [[EKEventStorealloc]init];
if([eventStorerespondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
[eventStorerequestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {
if(error) {
NSLog(@"添加失败...");
}elseif(!granted) {
NSLog(@"没有访问权限");
}else{
EKEvent*event = [EKEventeventWithEventStore:eventStore];
event.title= title;
event.location= description;//地址作为描述信息
//event.notes = description;
NSDateFormatter*formatter = [[NSDateFormatteralloc]init];
[formattersetDateFormat:@"HH:mm"];
NSDate*date = [formatterdateFromString:time];
//开始时间(必须传)
event.startDate= [datedateByAddingTimeInterval:0];
//结束时间(必须传)
event.endDate= [datedateByAddingTimeInterval:60*60];
//全天的事件
//event.allDay = YES;
//重复规则
EKRecurrenceRule *rule = [[EKRecurrenceRule alloc]initRecurrenceWithFrequency:EKRecurrenceFrequencyDaily interval:1 daysOfTheWeek:nil daysOfTheMonth:nil monthsOfTheYear:nil weeksOfTheYear:nil daysOfTheYear:nil setPositions:nil end:nil];
event.recurrenceRules=@[rule];
//添加提醒
//第一次提醒 设置事件开始之前5分钟提醒
[eventaddAlarm:[EKAlarmalarmWithRelativeOffset:-60.0*5]];
//第二次提醒 设置事件开始之前2分钟提醒
// [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -2.0f]];
//第N次提醒 设置事件提醒
// [event addAlarm:[EKAlarm alarmWithRelativeOffset:秒]];
//添加事件到日历中
EKCalendar*clendar = [eventStoredefaultCalendarForNewEvents];
[eventsetCalendar:clendar];
NSError*err;
[eventStoresaveEvent:eventspan:EKSpanFutureEventserror:&err];
if(!error) {
NSLog(@"添加日程成功");
//保存事件id,方便查询和删除
[[NSUserDefaultsstandardUserDefaults]setObject:event.eventIdentifierforKey:@"eventId"];
}
}
}];
}
}
//删除日程
- (BOOL)deleteCalendarEventIdentifier:(NSString*)eventIdentifier{
NSString*eIdentifier = [[NSUserDefaultsstandardUserDefaults]objectForKey:eventIdentifier];
EKEvent*event;
NSError*error =nil;
if(eIdentifier && ![eIdentifierisEqualToString:@"eventId"]) {
event = [self.eventStoreeventWithIdentifier:eIdentifier];
return [self.eventStore removeEvent:event span:EKSpanThisEvent error:&error];
}
return NO;
}