1 Reply
      Latest reply on Apr 3, 2020 7:31 PM by OOPer
      tantares Level 1 Level 1 (0 points)

        Hi, I have a Swift 5 project which accesses the user Calendar on a Mac Mojave via EKEventstore. I use the following code to initializes the access to the calendar:

         

        var eventStore = EKEventstore()
        
        
        func requestAccessToCalendar() {
                 eventStore.requestAccess(to: .event, completion: {
                           granted, error in      
                           })
                }
        func loadCalendars() {
                calendars = eventStore.calendars(for: .event)
                }
        
        
        
        let ekstatus = EKEventStore.authorizationStatus(for: EKEntityType.event)
                switch (ekstatus) {
                case EKAuthorizationStatus.notDetermined:
                    // This happens on first-run
                    requestAccessToCalendar()
                case EKAuthorizationStatus.authorized:
                    // Things are in line with being able to fetch all client events in the next period upfront
                    loadCalendars()
                    getFutureVisits()
                   
                case EKAuthorizationStatus.restricted, EKAuthorizationStatus.denied:
                    // We need to help them give us permission
                    dialogOK("access denied")
                @unknown default:
                    dialogOK("general access problem")
                }

         

        On the first run, the app logs permission problems in the XCode console, then asks the user to grant permission, which is granted but no calendar entries found. Any subsequent runs work fine as expected. The errors are reported directly when the eventStore instance is initialized. II've tried several options with lazy vars or constants, but all resulted in the same problem. The Apple developper documents unfortunatly show outdated ways to init the EKEventstore and I found nowhere a solution which works with Swift 5 and OS 10.14

         

        Did anyone succeed to have the instance initiated the first time without errors? I would appreciate a code sample.

        • Re: Swift EKEventstore init error
          OOPer Level 8 Level 8 (6,145 points)

          Please do not show just some fragments of codes.

          It is very important where those lines exist, whether the variables are properties or local variables.

           

          Please show whole code of the methods and the class where your code exists.