我似乎無法弄清楚爲什麼下面的代碼不工作。我得到了「創建日曆」和「日曆已存在」的NSlog,我也可以獲得沒有問題的日曆ID。我有一堆(如10)的創建日曆只是「出現」在一點,但我刪除了他們所有不斷嘗試,他們沒有顯示出來。我很困惑,爲什麼它不起作用。iOS EKEventStore不保存日曆
-(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");
}
}];
} }
可以請你爲「重置日曆」行動提供的代碼塊? –
這裏我的代碼是一樣的東西: FUNC resetCalendar(){ 做{ self.eventStore.reset() 試self.eventStore.commit() } 趕上{ 打印(」錯誤正在重置日曆== = >> \(error)「) showErrorMessageAlertView(error as NSError,viewdelegate:nil) } self.eventStore.refreshSourcesIfNecessary() } –