Howard Matis
Howard Matis

Reputation: 51

Unable to request permission to access the IOS Calendar in Swift 4.2

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

}

Upvotes: 5

Views: 6263

Answers (3)

cemtelliagaoglu
cemtelliagaoglu

Reputation: 1

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

Upvotes: 0

aerlich
aerlich

Reputation: 1

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

Upvotes: 0

Natarajan
Natarajan

Reputation: 3271

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.

Upvotes: 8

Related Questions