Swift EKEventstore init error

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.

Replies

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.

Hi, did you ever resolve this, I got the same issue on the first run of the app it asks for permission which is granted, but no calendar event are provided (0 items) , when the app restarts it works fine.