【问题标题】:EKCalendar title returns null ios 11EKCalendar 标题返回 null ios 11
【发布时间】:2017-09-14 06:43:03
【问题描述】:

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

NSArray *availablePersonalCalendars = [eventStore calendarsForEntityType:EKEntityTypeEvent];

for (EKCalendar *cal in availablePersonalCalendars) {

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

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

【问题讨论】:

    标签: objective-c null title ios11 ekcalendar


    【解决方案1】:

    我已使用此代码并在 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 *store 定义为类变量而不是局部变量来解决的。
    【解决方案2】:

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

    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
    

    斯威夫特

    let store = EKEventStore()
    var calendars: [EKCalendar] = []
    
    func loadCalendars() {
      calendars = store.calendars(for: .event)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 2022-06-15
      • 2018-08-21
      相关资源
      最近更新 更多