我正在尝试设置一个EKEventStore以(在他们的允许下)在iOS应用中显示用户的日历事件。
关于如何初始化EKEventStore实例(1. Accessing the Event Store; 2。Reading and Writing Calendar Events),我遇到的第一批Apple文档页面说的是这样的:
EKEventStore *eventStore = [[EKEventStore alloc] initWithAccessToEntityTypes:EKEntityMaskEvent];
但是,在使用最新的Xcode(版本11.3)和“单页应用程序”模板创建的全新iOS应用程序中,将上述代码行添加到ViewController.m的viewDidLoad
方法中,Xcode会出现此错误:
'initWithAccessToEntityTypes' is unavailable: not available on iOS
在iOS应用中初始化EKEventStore
的正确方法是什么?
文章EKEventStore表示在iOS上,您只能在iOS上使用默认的init
方法:
在macOS中,使用initWithAccessToEntityTypes:代替默认的init方法。可接受的实体类型是用于事件的EKEntityMaskEvent和用于提醒的EKEntityMaskReminder。
我尝试在我的应用的ViewController.m中这样做:
#import <EventKit/EventKit.h>
// ...
- (void)viewDidLoad
{
EKEventStore *eventStore = [[EKEventStore alloc] init];
// ...
}
这对我的iOS应用程序确实很有效;我能够继续使用eventStore
成功检索日历事件(在用户在运行时授予权限之后)。