Reputation: 690
I have to access user contacts from his phone book and show it in skstableview. For getting contact and showing it on tableview I am using below code but neither it is not asking for accessing contact permission nor getting contacts and print this statement on console:
"Access Denied" UserInfo={NSLocalizedDescription=Access Denied, NSLocalizedFailureReason=This application has not been granted permission to access Contacts.}
I have done entry in plist file for accessing contacts but nothing is working for me.
Here is the code snippet which I am using in Xcode 8.3.3 in swift.
func getContacts() {
let store = CNContactStore()
if CNContactStore.authorizationStatus(for: .contacts) == .notDetermined {
store.requestAccess(for: .contacts){succeeded, err in
guard err == nil && succeeded else{
return
}
self.contactList()
}
} else if CNContactStore.authorizationStatus(for: .contacts) == .authorized {
self.contactList()
NSLog(" move to contact list ")
}
}
func contactList() {
let req = CNContactFetchRequest(keysToFetch: [
CNContactFamilyNameKey as CNKeyDescriptor,
CNContactGivenNameKey as CNKeyDescriptor,
CNContactPhoneNumbersKey as CNKeyDescriptor
])
arr_contacts.removeAllObjects()
try! CNContactStore().enumerateContacts(with: req) {
contact, stop in
print("contacts \(contact)") // in real life, probably populate an array
self.arr_contacts.add(contact)
}
tbl_List.reloadData()
print("added all contacts")
}
Can any one suggest what am I doing wrong.
Thanks
Upvotes: 3
Views: 7241
Reputation: 5017
In case anybody arrives here with the following error (as I did): This application has not been granted permission to access some of the requested keys.
This may be because you're requesting the CNContactNoteKey
when fetching contacts. If that's the case, then you need a special permission in order to access the contact notes: https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_contacts_notes?language=objc
There's currently a bug in iOS15 betas that will throw an exception unless you request Notes
when fetching contacts, but the fetch will then fail if you do request Notes
. So it is effectively impossible to fetch contacts in iOS15 betas unless you have the special permission. I've reported this to Apple (FB9263783).
Upvotes: 3
Reputation: 59
Please Add this permission in your info.plist file.
<key>NSContactsUsageDescription</key>
<string>App users your contacts</string>
Upvotes: 2
Reputation: 690
This issue is on simulator it is working well in device and showing me access permission popup too.
Thanks guys for your great help.
Upvotes: 0
Reputation: 1754
Please add this to info.plist
<key>NSContactsUsageDescription</key>
<string>App users your contacts</string>
Upvotes: 4