2

My Mac application accessing the user calendar shows a permission access error on the first run before asking the user for Calendar access permission. The message does not appear on later runs. How can I prevent it? I use the following code:

class MyCalendar {
   let store = EKEventStore()
   init() {
      func requestAccessToCalendar() {           
          store.requestAccess(to: .event, completion:
            {(granted: Bool, error: Error?) -> Void in
          })
      }
      func loadCalendars() {
          calendars = store.calendars(for: .event)
      }

      let ekstatus = EKEventStore.authorizationStatus(for: EKEntityType.event)
      switch (ekstatus) {
         case EKAuthorizationStatus.notDetermined:
            requestAccessToCalendar()
         case EKAuthorizationStatus.authorized:      
            loadCalendars()         
         case EKAuthorizationStatus.restricted, EKAuthorizationStatus.denied:            
            dialogOK("access to Calendar denied")
         @unknown default:
            dialogOK("Unknown Calendar access problem")
         }
   }
}

The error occurs before any statement in the init() is executed. I also tried with a lazy var instead of a constant for store - without success. The plist entitlement is properly set an the application works as expected beside this error messages.

What is my mistake?

Tony
  • 83
  • 4
  • I did some more debugging and found that the error is raised as soon as the instance of the EKEventStore is initialised. This will occur latest at the call to requestAccess, when we first use the instance. – Tony Mar 26 '20 at 13:23

1 Answers1

0

I struggled with this for a while as well. As hinted elsewhere, you likely need to add the reason to the Info.plist, e.g.:

<dict>
    <key>NSCalendarsUsageDescription</key>
    <string>Export event information</string>
</dict>
  • Hi Philip, interestingly I have already set the reason in info.plist without success. Maybe it depends on special words in the string. – Tony Feb 12 '22 at 10:27
  • What is the error exactly? In general, the content of the string shouldn't matter. Perhaps using a DispatchSemaphore can help if it only happens during the very first run.. – Philip Makedonski Feb 12 '22 at 18:09