(iOS Swift) Share EKEventStore and access between app and Today Extension

506 Views Asked by At

I created an app that is mainly used by a Today Extension. It has to have access to the calendar because the widget displays upcoming events. Now I want the "host app" to have a list of all the users calendars and lets the user decide which calendars the widget should consider getting the events from.

My understanding is that the EKEventStore() initializer should not be called multiple times (by the widget and the host app in this example). Is there an easy way to only ask once for the permission to use the calendar (regardless whether the user first interacts with the host app or the widget) and share the permissions or do I have to check for permission in both the widget and the host app? Also, how should I ideally deal with using the EKEventStore() initializer in both the widget and the app?

1

There are 1 best solutions below

1
On

To answer the questions for future reference: This is my solution to the problem of only writing the code to check for calendar access once. I created an extension of EKEventStore class which lets me call a function which returns the status as a boolean. This makes usage of this in different targets much more clean

public extension EKEventStore {

func hasCalendarAccess() -> Bool {
    let status = EKEventStore.authorizationStatusForEntityType(EKEntityType.Event)
    var accessGranted : Bool = false

    switch(status){
    case EKAuthorizationStatus.NotDetermined:
        requestAccessToEntityType(EKEntityType.Event, completion: {
            (returnValue: Bool, error: NSError?) in
            accessGranted = returnValue
        })
    case EKAuthorizationStatus.Authorized:
        accessGranted = true
    case EKAuthorizationStatus.Denied:
        accessGranted = false
    case EKAuthorizationStatus.Restricted:
        print("restricted")
        accessGranted = false
    }

    return accessGranted
}

Thanks to dan for the answer regarding multiple instances of EKEventStore