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
This issue is on simulator it is working well in device and showing me access permission popup too.
Thanks guys for your great help.