Mike Crittenden
Mike Crittenden

Reputation: 5917

Can't request calendar event access (via EKEventStore) in Swift app for macOS

(I've never used Swift or Xcode before so this is probably a stupid question)

To isolate the issue, I created a totally empty "App" in Xcode 11, and put this into the AppDelegate.swift:

import Cocoa
import EventKit

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    private let eventStore = EKEventStore()

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        eventStore.requestAccess(to: .event) { granted, error in
            if granted {
                print("GOOD: Access granted")
            } else {
                print("BAD: Access denied")
            }
        }
    }
}

When I run the app (via cmd+R), I immediately see that it denied access, without ever giving me the chance to approve access:

Metal API Validation Enabled
BAD: Access denied

What am I doing wrong? How can I get it to allow me to approve access to calendar events?

Note that I noticed that the docs say:

To access the user’s Calendar data, all sandboxed macOS apps must include the com.apple.security.personal-information key.

So I made that change like so in App_Name.entitlements but it didn't help:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.security.personal-information</key>
    <true/>
    <key>com.apple.security.app-sandbox</key>
    <true/>
    <key>com.apple.security.files.user-selected.read-only</key>
    <true/>
</dict>
</plist>

Upvotes: 2

Views: 1395

Answers (2)

Peter Karsai
Peter Karsai

Reputation: 99

On macOS Sonoma, you also need to set Privacy - Calendars Full Access Usage Description in your Info.plist if your app requests full access.

Upvotes: 0

Mike Crittenden
Mike Crittenden

Reputation: 5917

Fixed. 2 things I had wrong:

  1. com.apple.security.personal-information in the entitlements file was incorrect. Just typing "Calendar" autocompletes to the correct one.
  2. In the Info.plist file, I needed "Privacy - Calendars Usage Description" to have a value. I found that here: https://forums.developer.apple.com/thread/110825

Upvotes: 5

Related Questions