Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CNPropertyNotFetchedException App Crashed

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.

like image 380
Muhammad Haroon Avatar asked Mar 29 '17 14:03

Muhammad Haroon


People also ask

How to fix nodemon app crashed error?

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.

What to do when your node app crashes?

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.

Why am I getting an appcrash error?

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?

What does program event name appcrash mean?

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.


1 Answers

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

like image 141
Ashley Mills Avatar answered Oct 08 '22 08:10

Ashley Mills