requestAccessToEntityType:completion: has been deprecated calling this is no longer allowed. Instead use requestFullAccessToEventsWithCompletion:

Advertisements

Today, I have updated my Xcode to 15.0 and my iPhone to iOS 17, but now my code that writes EKEvent in the calendar does not work anymore. It was working fine in earlier versions of iOS. Do I have a compatibility issue or is there another reason?

The error is this:

Error: -requestAccessToEntityType:completion: has been deprecated-calling this method is no longer allowed. Instead, use -requestFullAccessToEventsWithCompletion:, -requestWriteOnlyAccessToEventsWithCompletion:, or -requestFullAccessToRemindersWithCompletion:

My code looks like this below snippet:

    func accesCalenderToAddEvent() {
        let eventStore: EKEventStore = EKEventStore()
        eventStore.requestAccess(to: EKEntityType.event) { granted, error in
            DispatchQueue.main.async {
                if (granted) && (error == nil) {
                    let event = EKEvent(eventStore: self.eventStore)
                    event.title = "Event 1"
                    event.startDate = Date()
                    event.endDate = Date()
                    
                    let eventController = EKEventEditViewController()
                    eventController.event = event
                    eventController.eventStore = eventStore
                    eventController.editViewDelegate = self
                    self.present(eventController, animated: true, completion: nil)
                } else {
                    let alertController = UIAlertController(title: "Calendar Permission Required", message: "Please enable Calender permissions in settings.", preferredStyle: UIAlertController.Style.alert)
                    let okAction = UIAlertAction(title: "OK", style: .default, handler: {(cAlertAction) in
                        //Redirect to Settings app
                        UIApplication.shared.open(URL(string:UIApplication.openSettingsURLString)!)
                    })
                    alertController.addAction(okAction)
                    self.present(alertController, animated: true, completion: nil)
                }
            }
        }
    }

Please suggest me the solution for iOS 17 because in earlier version it’s work for me.

Thanks in advance.

>Solution :

The permissions scope for iOS 17 has changed to include Read, write, and full access, unlike iOS 16 which only had full access. This causes an error when you use eventStore.requestAccess() on iOS 17, but not on iOS 16.

Deprecation notice: https://developer.apple.com/documentation/eventkit/ekeventstore/1507547-requestaccesstoentitytype

More info: https://developer.apple.com/documentation/eventkit/ekeventstore/4162272-requestfullaccesstoeventswithcom

To provide support in iOS 17 write code like the below:

if #available(iOS 17.0, *) {
    eventStore.requestWriteOnlyAccessToEvents { granted, error in
        DispatchQueue.main.async {
            //write the existing logic here
        }
    }
}else {
    //put the old code here so it’s been working in earlier versions before iOS 17.
}

Note:

If you are only using EKEventEditViewController then you no need
to take the any permission as per this official doc: Documentation

Leave a ReplyCancel reply