我使用BOOL方法在自定义日历中保存事件,strForCalanderID
具有calendarIdentifier
值,但该事件不会保存在特定日历中。当我在默认日历上添加事件而不是在自定义或用户创建的日历中添加事件时,它工作正常。
-(BOOL)addEventOnICal:(NSMutableDictionary*)eventDict{
EKCalendar *calendar = nil;
NSString *calendarIdentifier =strForCalanderID;//strForCalanderID=> custom Calendar identifier
if (calendarIdentifier) {
calendar = [eventStore calendarWithIdentifier:calendarIdentifier];
calendar.title=strForCalanderName;
}
if (!calendar) {
// http://stackoverflow.com/questions/7945537/add-a-new-calendar-to-an-ekeventstore-with-eventkit
calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
for (EKSource *s in eventStore.sources) {
if (s.sourceType == EKSourceTypeLocal) {
calendar.source = s;
break;
}
}
NSString *calendarIdentifier = [calendar calendarIdentifier];
NSError *error = nil;
BOOL saved = [eventStore saveCalendar:calendar commit:YES error:&error];
if (saved) {
} else {
// unable to save calendar
return NO;
}
}
// this shouldn't happen
if (!calendar) {
return NO;
}
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
event.location = @"Indore";
event.title = @"Hello Test";
// set the start date to the current date/time and the event duration to two hours
event.startDate = StartDate;
event.endDate = EndDate;
NSError *error = nil;
// save event to the callendar
BOOL result = [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];
if (result) {
return YES;
} else {
// NSLog(@"Error saving event: %@", error);
// unable to save event to the calendar
return NO;
}
}