When I try to call [newEventStore defaultCalendarForNewEvents], it returns an error message says:
[707:907] defaultCalendarForNewEvents failed: Error Domain
iOS6+
requires users authentication to save event to his device calendar.
Here is a code snippt:
// save to iphone calendar
EKEventStore *eventStore = [[EKEventStore alloc] init];
if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
// iOS 6 and later
// This line asks user's permission to access his calendar
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (granted) // user user is ok with it
{
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = aTitle;
event.allDay = YES;
NSDateFormatter *dateFormat = [[UIApplicationSingleton sharedManager] aDateFormatter];
[dateFormat setDateFormat:@"MMM dd, yyyy hh:mm aaa"];
event.startDate = event.endDate = //put here if start and end dates are same
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
if(err)
NSLog(@"unable to save event to the calendar!: Error= %@", err);
}
else // if he does not allow
{
[[[UIAlertView alloc]initWithTitle:nil message:alertTitle delegate:nil cancelButtonTitle:NSLocalizedString(@"plzAlowCalendar", nil) otherButtonTitles: nil] show];
return;
}
}];
}
// iOS < 6
else
{
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = aTitle;
event.allDay = YES;
NSDateFormatter *dateFormat = [[UIApplicationSingleton sharedManager] aDateFormatter];
[dateFormat setDateFormat:@"MMM dd, yyyy hh:mm aaa"];
event.startDate = event.endDate = //put here if start and end dates are same
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
if(err)
NSLog(@"unable to save event to the calendar!: Error= %@", err);
}
And check my this post if you are facing trouble in setting alarm to the application.
I had the same problem, but finally find what was the reason.
My case was to add my Reminder and Calendar events, but I was using one EKEventStore
. In the end I seperate them and problem dissapeared:
private static let calendarEventStore = EKEventStore()
private static let remindersEventStore = EKEventStore()
So now I'm using calendarEventStore
for all things related to calendar event and remindersEventStore
for reminder one.
——
In my opinion it was related to the fact that I requested defaultCalendarForNewEvents
and defaultCalendarForNewReminders()
in one EKEventStore
entity.
Also this one from EKEventStore
docs:
Events, Reminders, and Calendar objects retrieved from an event store cannot be used with any other event store