swiftmacoseventkit

Error happens during creating reminder on MacOS using Swift


I have universal App for iOS and MacOS on SwiftUI. I need to create reminder. I have a code that works on iOS:

import EventKit 

func checkRemindersPermission() {
    let eventStore = EKEventStore()
    eventStore.requestAccess(to: EKEntityType.reminder, completion: { granted, error in
        if granted {
            createReminder()
        } 
    })
}

func createReminder() {
    let eventStore = EKEventStore()
    let alarm = EKAlarm(absoluteDate: localDate)
    let reminder = EKReminder(eventStore: eventStore)
    reminder.title = reminderTitle
    reminder.addAlarm(alarm)
    reminder.calendar = eventStore.defaultCalendarForNewReminders()
        
    do {
        try eventStore.save(reminder, commit: true)
    } catch {

    }
}

In Info I have NSRemindersUsageDescription key. I see Reminder permission window on MacOS and iOS. I can permit access to Reminder in MacOS (I see this permission in Setting) But when I try to create Reminder on MacOS I got error:

[CADXPCProxyHelper] Received error from calaccessd connection: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.apple.CalendarAgent was invalidated: failed at lookup with error 159 - Sandbox restriction." UserInfo={NSDebugDescription=The connection to service named com.apple.CalendarAgent was invalidated: failed at lookup with error 159 - Sandbox restriction.}. Attempting to call any reply handler.
[EventKit] Can't save reminder either because you have no access or you're trying to save using the legacy store, which is no longer supported
Error Domain=EKErrorDomain Code=29 "(null)"

Why this code works on iOS and nor work on MacOS?


Solution

  • As the documentation for EKEventStore states, sandboxed macOS apps must provide an extra entitlement that isn't needed on iOS, namely the com.apple.security.personal-information.calendars entitlement.

    To access the user’s Calendar data, all sandboxed macOS apps must include the com.apple.security.personal-information.calendars entitlement. To learn more about entitlements related to App Sandbox, see Enabling App Sandbox.

    For more info, see the EKEventStore docs.