次のコードが機能しない理由がわからないようです。「CalendarCreated」と「CalendarExisted」のNSlogを取得します。CalendarIDも問題なく取得します。作成したカレンダーの束(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");
}
}];
} }