首页 文章

Swift 4.0 Eventkit无法正确获取日历和事件

提问于
浏览
5

我正在尝试使用Eventkit来访问Mac日历 . 虽然我在应用程序中有本地日历,iCloud日历和Google日历中的多个日历和许多事件,但已成功请求访问但我仍然获得零或一系列日历或事件 .
我从以下代码得到的输出是:在我的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)

如果我尝试从代码创建并保存日历,那么我收到错误:错误Domain = EKErrorDomain Code = 5“当持久性不可用时尝试保存”UserInfo =

2 回答

  • 5

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

    您需要在权利文件中将 com.apple.security.personal-information.calendars 键设置为YES, even if your app is not sandboxed. Apple的EventKit实施中存在一个错误,如果未设置此密钥,即使沙箱被禁用,也会阻止您的应用访问日历 .

  • 0

    我可能错了,但是你在谓词中缺少一个参数,你传递的是nil,所以基本上没有任何输出排序 . 尝试将代码更改为:

    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: calendars)  //change here
        let events = self.eventStore.events(matching: predicate)
        print(calendars)
        print(events)
    

相关问题