Calendar Access Issues Using EKEventStore on OSX Sierra with Swift 3

It looks very simple, but I struggled for several days to access the calendar on OSX. I turned on the Application Sandbox feature, and I added a checkmark to the Calendar field in the Application Data. I created a very simple application with the following 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 the EventKit and initialize the eventControl.

When I run this while debugging, I get an error in the eventControl initialization 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 cannot figure out how to get the correct permissions.

I am using Xcode 8.1 and macOS Sierra 10.12.1

+4
1

- ? , - , Store nil. " ". :

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))
0

Source: https://habr.com/ru/post/1659136/


All Articles