5

I have tried the earlier examples of asking permission to add items to the IOS calendar. They do not work with Xcode 10.1 (Swift 4.2). When I try to compile the code, I get an error. If I comment out the lines beginning with "EKEventstore.requestAccess", the code executes the ".not.Determined" loop.

The error is "Instance member 'requestAccess' cannot be used on type 'EKEventStore'; did you mean to use a value of this type instead?"

Can anyone please find my error so that the IOS app can have permission to add events to the Calendar?

func SOGetPermissionCalendarAccess() {

    switch EKEventStore.authorizationStatus(for: .event) {

    case .authorized:
        print("Authorized")

    case .denied:
        print("Access denied")

    case .notDetermined:
        EKEventStore.requestAccess(to: .event, completion:
            {[weak self] (granted: Bool, error: Error?) -> Void in
                if granted {
                    print("Access granted")
                } else {
                    print("Access denied")
                }
        })

        print("Not Determined")
    default:
        print("Case Default")
        }

}
Howard Matis
  • 51
  • 1
  • 7

2 Answers2

8

You should create event store instance as like below,

let eventStore = EKEventStore()

After that you can make permission request as like below,

switch EKEventStore.authorizationStatus(for: .event) {

        case .authorized:
            print("Authorized")

        case .denied:
            print("Access denied")

        case .notDetermined:
            eventStore.requestAccess(to: .event, completion:
                {(granted: Bool, error: Error?) -> Void in
                    if granted {
                        print("Access granted")
                    } else {
                        print("Access denied")
                    }
            })

            print("Not Determined")
        default:
            print("Case Default")
        }

Please refer this link for more info.

Natarajan
  • 3,241
  • 3
  • 17
  • 34
  • This worked. The line after "eventStore.." should be " {(granted: Bool, error: Error?) -> Void in" because self is not used. – Howard Matis Nov 22 '18 at 17:25
  • Yeah correct. If you didn't use "self" inside that completion block, you can remove that part. – Natarajan Nov 23 '18 at 06:42
  • Is is somehow possible to programatically change authorization denied by user in macOS settings? Or reset app authorization to status .notDetermined? – Dawy Jan 18 '19 at 22:20
0

Apple documentation for EKEventStore is to execute reset() method, that does not help either. My workaround is to initialise the EKEventStore once again, after requestAccess method.

private var store: EKEventStore
private var calendars: [EKCalendar] = []

private func requestAccessCalendars() {
    store.requestAccess(to: .event) { [weak self] (accessGranted, error) in
        if accessGranted {
            self?.store = EKEventStore() // <- second instance
            self?.store.refreshSourcesIfNecessary()

            self?.loadCalendars()
        }
    }
}

private func loadCalendars() {
    let cals = store.calendars(for: .event)
    for c in cals {
        if c.allowsContentModifications { // without birthdays, holidays etc'...
            calendars.append(c)
        }
    }
}
aerlich
  • 1
  • 1