此代码在ios 11之前一直运行良好,但现在在ios 11中,ID工作正常,但标题返回null。
NSArray *availablePersonalCalendars = [eventStore calendarsForEntityType:EKEntityTypeEvent];
for (EKCalendar *cal in availablePersonalCalendars) {
NSLog(@"ID: %@", cal.calendarIdentifier);
NSLog(@"Title: %@", cal.title)
}
如果您知道如何解决此问题,请帮助我。 谢谢,
答案 0 :(得分: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 键,并附有解释性文本,说明如何使用此信息。
答案 1 :(得分:1)
您应存储对您的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)
}