EKCalendar 标题 returns 空 ios 11

EKCalendar title returns null ios 11

此代码在 ios 11 之前工作正常,但现在在 ios 11 中 ID 工作正常但标题 returns 无效。

NSArray *availablePersonalCalendars = [eventStore calendarsForEntityType:EKEntityTypeEvent];

for (EKCalendar *cal in availablePersonalCalendars) {

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

如果你知道如何解决这个问题,请帮助我。 谢谢,

我已经使用此代码并且在 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);
       }
  }];

还要确保在 plist 中包含 NSCalendarsUsageDescription 键,以及如何使用此信息的解释性文本。

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

您应该存储对您的 EKEventStore object 的引用。不知何故,它与缺少日历的标题有关。不要忘记在查询日历之前请求权限。

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)
}