eventkitekeventekeventstoreekeventkit

How to Get Calendar Permissions with EventKit in Swift in macOS App


I'm trying to add a calendar event to the macOS calendar from a Mac app I'm developing with Swift 5 in Xcode 11.2.1.

For starters, I have the Calendar entitlement checked in the App Sandbox.

I'm trying to prompt for permissions like this:

import EventKit

@IBAction func clickAddToCalendar(_ sender: NSButton){
  let eventStore = EKEventStore()
  eventStore.requestAccess(to: .event){ granted, error in
    print(granted) // returns false
    print("----")
    print(error) // returns nil      
  }
}

...but granted is false every time and I never see a popup to grant permission. Is prompting for access like this necessary on macOS? It would seem so, because when I attempt to add a calendar event directly like this:

let event = EKEvent(eventStore: eventStore)

event.title = "My Event"
event.startDate = Date()
event.endDate = Date()
event.calendar = eventStore.defaultCalendarForNewEvents

do {
  try eventStore.save(event, span: .thisEvent)
} catch let error as NSError {
  print(error)
}

...I get an error:

serviceName = "com.apple.CalendarAgent.database"; ... returned error NSCocoaErrorDomain(134070) with userInfo dictionary { Problem = "request failed, insufficient permission" }

Has anyone does this that could give me some pointers? Thank you!


Solution

  • Do you have an info.plist item for Privacy - Calendars Usage Description?

    required for EventKit see https://developer.apple.com/documentation/eventkit/accessing_the_event_store