Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}
like image 387
Howard Matis Avatar asked Nov 22 '18 06:11

Howard Matis


1 Answers

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.

like image 112
Natarajan Avatar answered Nov 10 '22 10:11

Natarajan