我似乎无法弄清楚为什么以下代码不起作用。我获得了“日历已创建”和“日历已存在”的 NSlogs 我也毫无问题地获得了日历 ID。我有一堆(比如 10 个)创建的日历只是“出现”一次,但我一直在尝试删除它们,但它们没有出现。我真的很困惑为什么它不起作用。
-(void)saveEventWithDate:(NSDate *)startDate endDate:(NSDate *)endDate {
AppData *theData = [self theAppData];
EKEventStore *eventStore = [[EKEventStore alloc] init];
if([self checkIsDeviceVersionHigherThanRequiredVersion:@"6.0"]) {
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted){
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"Calendar"] == nil) // Create Calendar if Needed
{
EKSource *theSource = nil;
for (EKSource *source in eventStore.sources) {
if (source.sourceType == EKSourceTypeCalDAV && [source.title isEqualToString:@"iCloud"]) {
theSource = source;
NSLog(@"iCloud Store Source");
break;
} else {
for (EKSource *source in eventStore.sources) {
if (source.sourceType == EKSourceTypeLocal) {
theSource = source;
NSLog(@"iPhone Local Store Source");
break;
}
}
}
}
EKCalendar *cal;
cal = [EKCalendar calendarWithEventStore:eventStore];
cal.title = @"hello";
cal.source = theSource;
[eventStore saveCalendar:cal commit:YES error:nil];
NSLog(@"cal id = %@", cal.calendarIdentifier);
NSString *calendar_id = cal.calendarIdentifier;
[defaults setObject:calendar_id forKey:@"Calendar"];
event.calendar = cal;
} else {
event.calendar = [eventStore calendarWithIdentifier:[defaults objectForKey:@"Calendar"]];
NSLog(@"Calendar Existed");
}
event.title = [NSString stringWithFormat:@"%@ iPhone",[theData.repair_info objectForKey:@"name"]];
event.location = @"Location of";
event.notes = @"Notes";
event.startDate = startDate;
event.endDate = endDate;
event.allDay = NO;
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:-1800]; // Half Hour Before
event.alarms = [NSArray arrayWithObject:alarm];
[eventStore saveEvent:event span:EKSpanThisEvent error:nil];
} else {
NSLog(@"Not Granted");
}
}];
} }