Problems accessing Calendar using EKEventStore on OSX Sierra with Swift 3

为君一笑 提交于 2021-02-07 05:20:42

问题


This appears very simple, but I've been struggling for several days to get access to the Calendar on OSX. I have switched on the App Sandbox capability, and I've ticked the "Calendar" box in App Data. I have created very simple app with the following view controller class:

import Cocoa
import EventKit

class ViewController: NSViewController {

    var eventControl = EKEventStore()
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }
}

As you can see, the only lines of code I've added are to import EventKit and to initialise eventControl.

When I run this in debug, I get an error at the eventControl initialisation line

2016-10-28 15:02:00.056521 calendarTest[4105:847101] CoreData: XPC:     Unable to load metadata: Error Domain=NSCocoaErrorDomain Code=134070 "An error occurred in the persistent store." UserInfo={Problem=request failed, insufficient permission}
2016-10-28 15:02:00.057742 calendarTest[4105:847101] [error] error: -addPersistentStoreWithType:NSXPCStore configuration:(null) URL:file:///Users/patrickramsden/Library/Calendars/Calendar%20Cache  options:{
    NSInferMappingModelAutomaticallyOption = 1;
    NSMigratePersistentStoresAutomaticallyOption = 1;
    agentOrDaemon = 1;
    serviceName = "com.apple.CalendarAgent.database";
} ... returned error Error Domain=NSCocoaErrorDomain Code=134070 "An error occurred in the persistent store." UserInfo={Problem=request failed, insufficient permission} with userInfo dictionary {
    Problem = "request failed, insufficient permission";
}

I can't work out how to get the right permissions.

I am using Xcode 8.1 and macOS Sierra 10.12.1


回答1:


You need to add a usage description to your info.plist. This is a small description of why you need access to the services that is presented to the user.

<key>NSCalendarsUsageDescription</key>
<string>Description of why you need access to the Calendar</string>



回答2:


Has anyone else figured this out? I am running into the same issue, however trying to access anything in the event Store returns nil. Even though "access is granted". Such as:

let eventStore = EKEventStore()
switch EKEventStore.authorizationStatus(for: .event) {
  case .authorized:
    print("Access Granted")
    break
  case .denied:
    print("Access denied")
  case .notDetermined:
    eventStore.requestAccess(to: .event, completion:
      {(granted: Bool, error: Error?) -> Void in
        if granted {
          print("Access Granted")

        } else {
          print("Access denied")
        }
    })
    break
  default:
    print("Case Default")
  break
}

print(eventStore.calendars(for: .event))



回答3:


Getting the same messages: How to prevent EventStore access error on first run

However the access is working correctly once you start the application again after the user grants permission! My app is not sandboxed. Have you set the "Privacy - Calendars Usage Description" key in your plist?



来源:https://stackoverflow.com/questions/40298000/problems-accessing-calendar-using-ekeventstore-on-osx-sierra-with-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!