Authorizing Contact Access
suggest changeImporting the framework
Swift
import Contacts
Objective-C
#import <Contacts/Contacts.h>
Checking accessibility
Swift
switch CNContactStore.authorizationStatusForEntityType(CNEntityType.Contacts) {
case .Authorized:
break // access contacts
case .NotDetermined:
break // request permission; the prompt is shown only in this state
case .Denied, .Restricted:
break // the system will not show the prompt again
}
Objective-C
switch ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]) {
case CNAuthorizationStatusAuthorized:
// access contacts
break;
case CNAuthorizationStatusNotDetermined:
// request permission; the prompt is shown only in this state
break;
case CNAuthorizationStatusDenied:
case CNAuthorizationStatusRestricted:
// the system will not show the prompt again
break;
}
Requesting Permission
Swift
let contactStore = CNContactStore()
contactStore.requestAccessForEntityType(CNEntityType.Contacts, completionHandler: { granted, error in
if granted {
// access contacts
}
})
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents