14

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

PatrickR
  • 143
  • 6
  • After lots of further testing, it seems that although this error is reported, the code still executes correctly. `eventControl` is being set to a valid object, and I am able to execute EventKit operations on that object successfully. I believe this error does not cause any problems. – PatrickR Nov 02 '16 at 02:42
  • For me the error is not a false positive, it just never runs the callback (not at all), I would have expected it to run the callback with an error, but that doesn't seem to happen. I think there might be just something broken with permissions requests for terminal programs. One of the weirder things I noticed, only when I ran the program outside of XCode's debugger (i.e., when I ran it from the terminal) macOS asked me for contacts permissions (???) even though I was requesting reminder permissions? Even after I granted it, it seemed to have no effect at all, same error. – jrh Jan 24 '21 at 01:05

3 Answers3

11

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>
Cory
  • 2,302
  • 20
  • 33
  • 1
    I think this is for ios, and it does not work for me anyway. I had an app working perfectly on Sierra and since i updated to Mojave i get this error. The thing is i do not even call the code, but when i do i get that the authorization is not determined, although in privacy i have granted xcode. – Cristi Băluță Nov 18 '18 at 21:04
  • same issue as @CristiBăluță – alexdd55 Nov 19 '18 at 15:58
  • Btw, i am confused a little, i knew that when apps are run from xcode the request for granting access comes from xcode, but after a while and some relaunches it started to work and the access is granted for the app. – Cristi Băluță Nov 19 '18 at 16:17
  • If you have HardenedRuntime and AppSandbox - calendar needs to be checked there too. – Krzysztof Jan 15 '20 at 19:25
  • This didn't seem to have any effect for me. – jrh Jan 24 '21 at 01:02
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))
Jeff Brown
  • 86
  • 8
0

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?

Tony
  • 83
  • 4