2

此代码在 ios 11 之前一直运行良好,但现在在 ios 11 中,ID 运行良好,但标题返回 null。

NSArray *availablePersonalCalendars = [eventStore calendarsForEntityType:EKEntityTypeEvent];

for (EKCalendar *cal in availablePersonalCalendars) {

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

如果您知道如何解决此问题,请帮助我。谢谢,

4

2 回答 2

2

我已使用此代码并在 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

于 2017-09-14T07:05:04.960 回答
1

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

目标-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

斯威夫特

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

func loadCalendars() {
  calendars = store.calendars(for: .event)
}
于 2018-10-08T15:02:58.450 回答