Unable to request permission to access the IOS Calendar in Swift 4.2
Asked Answered
R

3

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")
        }

}
Rombert answered 22/11, 2018 at 6:32 Comment(0)
N
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.

Nebiim answered 22/11, 2018 at 6:56 Comment(3)
This worked. The line after "eventStore.." should be " {(granted: Bool, error: Error?) -> Void in" because self is not used.Rombert
Yeah correct. If you didn't use "self" inside that completion block, you can remove that part.Nebiim
Is is somehow possible to programatically change authorization denied by user in macOS settings? Or reset app authorization to status .notDetermined?Protract
P
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)
        }
    }
}
Peavy answered 1/11, 2019 at 10:21 Comment(0)
C
0

With iOS17, the request(to:, completion:) method is deprecated with requestFullAccessToEvents(completion:) and requestWriteOnlyAccessToEvents(completion:) .

https://developer.apple.com/documentation/eventkit/ekeventstore/4162272-requestfullaccesstoevents

So updated version for request might be as following;

switch EKEventStore.authorizationStatus(for: .event) {
    case .fullAccess:
        print("Full Access")
    case .authorized:
        print("Authorized")
    case .writeOnly:
        print("Write Only Access")
    case .denied:
        print("Authorization Denied")
    case .notDetermined:
        if #available(iOS 17.0, *) {
            eventStore.requestFullAccessToEvents { isGranted, error in
                print("isGranted: ", isGranted)
            }
        } else {
            eventStore.requestAccess(to: .event) { isGranted, error in
                print("isGranted: ", isGranted)
            }
        }
    default:
        print("Default Case")
}
Criminal answered 26/3, 2024 at 12:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.