I'm having a strange issue EKEventStore, iCloud and local calendars. If iCloud is enabled the Calendar is created and events are saved into the calendar as you would expect. if iCloud is turned off and you try to save an event nothing happens, however the device continues to create iCloud calendars in a loop every 3-5 seconds until iCloud is turned back on and then all of those calendars flood into iCloud as duplicates. I'm using nearly the exact code that has been referenced here on SO many times as well as in Apples Docs. I'm completely stumped as to why it's not working and there seems to be very little documentation on EKEventStore in general.
//••••••••••••••••••••••••••••••••••••••••••••••• #pragma mark – Save Event //•••••••••••••••••••••••••••••••••••••••••••••••
-(void)saveEventWithDate:(NSDate *)startDate endDate:(NSDate *)endDate
{
AppData *theData = [self theAppData];
if([self checkIsDeviceVersionHigherThanRequiredVersion:@"6.0"]) {
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { // iOS 6 Support
if (granted){
NSLog(@"Access Granted");
} else {
NSLog(@"Access Not Granted");
}
}];
}
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([eventStore calendarWithIdentifier:[defaults objectForKey:@"My Calendar"]] != nil) // Calendar Existed
{
event.calendar = [eventStore calendarWithIdentifier:[defaults objectForKey:@"My Calendar"]];
NSLog(@"Calendar Existed");
} else { // Create Calendar
EKSource *theSource = nil;
for (EKSource* src in eventStore.sources) {
if ([src.title isEqualToString:@"iCloud"]) {
theSource = src;
break;
}
if (src.sourceType == EKSourceTypeLocal && theSource==nil) {
theSource = src;
break;
}
}
[self setupCalendarWithSource:theSource withEvent:event];
}
NSLog(@"Type of Event:%@",typeOfEvent);
if ([typeOfEvent isEqualToString:@"Hello"]) {
event.title = [NSString stringWithFormat:@"%@ Hello",[theData.hello_info objectForKey:@"customer_name"]];
event.location = [NSString stringWithFormat:@"Phone #%@",[theData.hello_info objectForKey:@"customer_phone_number"]];
event.notes = [NSString stringWithFormat:@"Hello Issue: %@",[theData.hello_info objectForKey:@"hello_issue"]];
NSLog(@"Hello");
}
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];
SAFE_PERFORM_WITH_ARG(_delegate, @selector(wasScheduled), nil);
}
-(void)setupCalendarWithSource:(EKSource *)theSource withEvent:(EKEvent *)event {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
EKCalendar *cal;
cal = [EKCalendar calendarWithEventStore:eventStore];
cal.title = @"My Appointments";
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:@"My Calendar"];
event.calendar = cal;
}