Swift 4.0 Eventkit无法正确获取日历和事件 [英] Swift 4.0 Eventkit Cannot get calendars and events correctly

查看:177
本文介绍了Swift 4.0 Eventkit无法正确获取日历和事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Eventkit访问Mac日历. 已成功请求访问权限,但是即使应用程序中本地日历,iCloud日历和Google日历中有多个日历和许多事件,我仍会得到零个日历或事件或空数组. 我从以下代码获得的输出是: 在我的Mac上 [] []

I'm trying to use Eventkit to access Mac Calendar. Access is successfully requested but I keep getting nil or an empty array of calendar or events, even though I have several calendars and many eventsin local calendar, iCloud calendar, and Google calendar in the app.
The output I get from the following code is: On My Mac [] []

    let sources = eventStore.sources
    for source in sources{
        print(source.title)
        for calendar in source.calendars(for: .event){
            print(calendar.title)
        }
    }

    let calendars = eventStore.calendars(for: .event)
    let predicate = self.eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: nil)
    let events = self.eventStore.events(matching: predicate)
    print(calendars)
    print(events)

如果我尝试通过代码创建并保存日历,则会收到错误消息: Error Domain = EKErrorDomain代码= 5尝试在持久性不可用时进行保存" UserInfo = {NSLocalizedDescription =试图在持久性不可用时进行保存}

And if I try to create and save a calendar from code, then I'm getting error: Error Domain=EKErrorDomain Code=5 "Attempted to save when persistence was unavailable" UserInfo={NSLocalizedDescription=Attempted to save when persistence was unavailable}

推荐答案

经过反复试验,我找到了答案.

After lots of trial and error, I found the answer.

即使未对应用程序进行沙箱处理,也需要在权利文件中将com.apple.security.personal-information.calendars键设置为是". Apple的EventKit实现中存在一个错误,该错误会阻止您的应用访问即使未启用沙箱,日历也不会设置此键.

You need to set the com.apple.security.personal-information.calendars key to YES in your entitlements file, even if your app is not sandboxed. There is a bug in Apple's implementation of EventKit that prevents your app getting access to calendars if it does not set this key, even if the sandbox is disabled.

这篇关于Swift 4.0 Eventkit无法正确获取日历和事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆