How to prevent EventStore access error on first run

99封情书 提交于 2021-01-24 09:48:11

问题


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?

来源:https://stackoverflow.com/questions/60851829/how-to-prevent-eventstore-access-error-on-first-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!