Josh O'Connor Josh O'Connor -4 years ago 107
iOS Question

Denied Contact access (Contacts framework, iOS)

I am using Contacts framework. I am having trouble enabling contacts if the user has switched it to off in the settings screen:

Settings app -> App name -> Turn Contacts off


If a user turns off the app Contact permissions in the settings screen, is it possible to present a dialog to allow them to turn Contacts on? It seems as if access = always false. Are we not allowed access once the user turns off Contacts via settings?

Here is my relevant code:

func requestForAccess(completionHandler: @escaping (_ accessGranted: Bool) -> Void) {
let authorizationStatus = CNContactStore.authorizationStatus(for: CNEntityType.contacts)

switch authorizationStatus {
case .authorized:
completionHandler(true)

case .denied, .notDetermined:
self.contactStore.requestAccess(for: CNEntityType.contacts, completionHandler: { (access, accessError) -> Void in
if access {
completionHandler(access)
}
else {
//Access is always false
if authorizationStatus == CNAuthorizationStatus.denied {
completionHandler(false)
}
}
})

default:
completionHandler(false)
}
}

Answer Source

No, once the user has denied access to Contacts, you'll need to direct them to the Settings app.

You can do this by displaying a UIAlertViewController, and opening the settings app via an alertAction

UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download