I'm having an issue where I create my EKCalendar and everything looks good but then when I go to list my calendars, it doesn't show up. I also go to check my calendar list in my calendar app but it is non existant. Any thoughts?
Here is my button code to create my calendar:
- (IBAction)one:(id)sender {
NSString* calendarName = @"My Cal";
EKCalendar* calendar;
// Get the calendar source
EKSource* localSource;
for (EKSource* source in eventStore.sources) {
if (source.sourceType == EKSourceTypeLocal)
{
localSource = source;
break;
}
}
if (!localSource)
return;
calendar = [EKCalendar calendarWithEventStore:eventStore];
calendar.source = localSource;
calendar.title = calendarName;
NSError* error;
bool success= [eventStore saveCalendar:calendar commit:YES error:&error];
if (error != nil)
{
NSLog(error.description);
// TODO: error handling here
}
NSLog(@"cal id = %@", calendar.calendarIdentifier);
}
And here is my button code to list the calendar, but my new calendar is never included!
- (IBAction)two:(id)sender {
NSArray *calendars = [eventStore calendarsForEntityType:EKEntityTypeEvent];
for (EKCalendar* cal in calendars){
NSLog(@"%@",cal.title);
}
}
Thank you in advance!
The solution Massimo Oliviero did not work for me. I had the same issue. I was creating Calendar after calling
requestAccessToEntityType
.What worked for me is after getting the permission I reinitialised the
EventStore
object. It was like the permissions were never upadted.In CalendarManager
Have you tried checking authorization setting first to make sure the user has given permission to access the store?
For
EKEventStore
documentation:Important: If your app has never requested access before, you must request access to events or reminders before attempting to fetch or create them. If you request data before prompting the user for access with this method, you'll need to reset the event store with the reset method in order to start receiving data once the user grants access.
The local store may not support events. This is reproducible if iCloud is enabled.
This is the most reliable solution I could find, without hard-coding any assumptions:
I found a solution. The problem is that when iCloud calendars switched on, it hides the locally created ones from the calendar app. To bypass this problem the solution is to add a new calendar to iCloud source:
I had this problem too. My solution is nearly like other answers, but I use another way to get the instance of EKSource.
As written in documentation:
So, I use this code to get the proper EKSource:
BUT if you use another calendar like Gmail (or Yahoo etc.), you can't add your calendar to its source.
In this rare case I use a full search:
I had this same problem. I did as sixthcent suggested and had the app ask for permission. That solved one part but the calendar didn't show up still but was being created according to the NSLogs. I had an Exchange calendar (I didn't have iCloud) on my phone and had to turn it off. Once that was deleted it showed up as a Local calendar. When I went back to re-add Exchange it asked if I wanted to keep both and now both calendars are showing up. Here is my code below.