i want to get the all contacts list of phone with their name as well as phone numbers in swift 3.0 and xcode 8.0. below is the code
func get_all_user_contacts()
{
let status = CNContactStore.authorizationStatus(for: .contacts)
if status == .denied || status == .restricted
{
presentSettingsActionSheet()
return
}
// open it
let store = CNContactStore()
store.requestAccess(for: .contacts) { granted, error in
guard granted else
{
DispatchQueue.main.async {
self.presentSettingsActionSheet()
}
return
}
// get the contacts
var contacts = [CNContact]()
let request = CNContactFetchRequest(keysToFetch: [CNContactIdentifierKey as NSString, CNContactFormatter.descriptorForRequiredKeys(for: .fullName)])
do
{
try store.enumerateContacts(with: request)
{ contact, stop in
contacts.append(contact)
}
}
catch
{
print(error)
}
// do something with the contacts array (e.g. print the names)
let formatter = CNContactFormatter()
formatter.style = .fullName
for contact in contacts
{
let MobNumVar = ((contact.phoneNumbers.first?.value)! as CNPhoneNumber).stringValue
print(MobNumVar)
print(formatter.string(from: contact) ?? "???")
}
}
}
when i run this app it crashes and i don't know where i am getting wrong. anyone can help me out.. it will be appreciated.
Hai Node Js developers it’s a fix for nodemon app crashed error .do you have an error like given below nodemon app crashed – waiting for file changes before starting… STEP 1: don’t worry dude it’s not a big deal. now check your node js version it’s supported or not. STEP 2. Check your terminal window. any node js script already running or not.
nodemon app crashed – waiting for file changes before starting…. STEP 1: don’t worry dude it’s not a big deal. now check your node js version it’s supported or not. STEP 2. Check your terminal window. any node js script already running or not. STEP 3: if any node js files are running close all terminal window.
Example, there might be some corrupt system files on the computer for the cause of the appcrash error. You also need to check the event viewer for additional information related to the issue and let us know about the same. Which web browser are you using?
The “Program Event Name: APPCRASH” shows up when a program does not work. For example, once inside the window with the message saying “xx has stopped working”, you might see the error message “Program Event Name: APPCRASH” after clicking the View problem details button.
You're requesting keys
• CNContactIdentifierKey
• CNContactFormatter.descriptorForRequiredKeys(for: .fullName)
…but then you're trying to access contact.phoneNumber
.
You can only access keys specified in keysToFetch
, so you need to add CNContactPhoneNumbersKey
to that array
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With