EKCalendar title returns null ios 11

477 Views Asked by At

This code has been working fine prior to ios 11, but now in ios 11 the ID works fine but the title returns null.

NSArray *availablePersonalCalendars = [eventStore calendarsForEntityType:EKEntityTypeEvent];

for (EKCalendar *cal in availablePersonalCalendars) {

    NSLog(@"ID: %@", cal.calendarIdentifier);
    NSLog(@"Title: %@", cal.title)
}

Please help me out if you know how to fix this. Thanks,

2

There are 2 best solutions below

1
Dailos Medina On BEST ANSWER

I have used this code and is working correctly in iOS 11:

EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent
                      completion:^(BOOL granted, NSError * _Nullable error) {
       NSArray *availablePersonalCalendars = [store calendarsForEntityType:EKEntityTypeEvent];

       for (EKCalendar *cal in availablePersonalCalendars) {

          NSLog(@"ID: %@", cal.calendarIdentifier);
          NSLog(@"Title: %@", cal.title);
       }
  }];

Also be sure to include in the plist the NSCalendarsUsageDescription key, with a explanatory text of how is going to be used this information.

https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW15

0
Tim Bernikovich On

You should store reference to your EKEventStore object. Somehow its related to missing calendars' titles. Dont forget to request permissions before querying calendars.

Objective-C:

@interface Some ()

@property (nonatomic) EKEventStore *store;
@property (nonatomic) NSArray<EKCalendar *> *calendars;

@end

@implementation Some

- (void)prepare
{
  self.store = [EKEventStore new];
  self.calendars = @[];
}

- (void)loadCalendars
{
  self.calendars = [self.store calendarsForEntityType:EKEntityTypeEvent];
}

@end

Swift:

let store = EKEventStore()
var calendars: [EKCalendar] = []

func loadCalendars() {
  calendars = store.calendars(for: .event)
}