Address Book crash on iOS10

倾然丶 夕夏残阳落幕 提交于 2019-12-06 00:10:38
imalice

Imran Raheem

From Erdekhayser's solution (Contact Address book crash on iOS 10 beta)

you can use this method to check CNContactPickerViewController is available?

if (NSClassFromString(@"CNContactPickerViewController")) {
        // iOS 9, 10, use CNContactPickerViewController
        CNContactPickerViewController *picker = [[CNContactPickerViewController alloc] init];
        picker.delegate = self;
        picker.displayedPropertyKeys = @[CNContactPhoneNumbersKey];
        [pr presentViewController:picker animated:YES completion:nil];
    }else{
        // iOS 8 Below, use ABPeoplePickerNavigationController
        ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
        picker.peoplePickerDelegate = self;
        [pr presentViewController:picker animated:YES completion:nil];
    }

The Address Book API was deprecated in iOS 9 in favor of the more object-oriented Contacts Framework.

Instead of using the ABPeoplePickerViewController, move to CNContactPickerViewController.

I was getting the same error, when I was trying to get an emailAddresses from CNContact of delegate method.

Initially, I initialize the contactpicker:

//MARK: Contact Action
@IBAction func getContactListAction(_ sender: Any) {

    let contactPicker = CNContactPickerViewController()
    contactPicker.delegate = self
    contactPicker.displayedPropertyKeys = [CNContactPhoneNumbersKey]

    vcObject.present(contactPicker, animated: true, completion: nil)
}

Delegate method:

//MAKE: Contact Delegate

func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
    picker.dismiss(animated: true, completion: nil)
    let name = CNContactFormatter.string(from: contact, style: .fullName)
    print(name!)
    self.textfieldName.text = name!


    for number in contact.phoneNumbers {

        print("number ----\(number)")

        let mobile = number.value.value(forKey: "digits") as? String
        if (mobile?.count)! > 7 {
            // your code goes here
            print("mobile---\(String(describing: mobile))")
            self.textfieldMobileNumber.text = mobile!
        }
    }


 // this line couse the crash ---> print(contact.emailAddresses[0].value(forKey: "value") as! String)

}

I was accessing the email address without declaring in initialization. Error -- Terminating app due to uncaught exception 'CNPropertyNotFetchedException', reason: 'A property was not requested when contact was fetched.'

CORRECT CODE FOR ACCESSING EMAIL ---

Xcode 10 . and 4.2

//MARK: Contact Action
@IBAction func getContactListAction(_ sender: Any) {

    let contactPicker = CNContactPickerViewController()
    contactPicker.delegate = self

    contactPicker.displayedPropertyKeys = [CNContactPhoneNumbersKey,CNContactEmailAddressesKey] . 
 // <--- Here make declaration for accessing the required property from CNContact.

    vcObject.present(contactPicker, animated: true, completion: nil)


}

//MAKE: Contact Delegate
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
    picker.dismiss(animated: true, completion: nil)
    let name = CNContactFormatter.string(from: contact, style: .fullName)
    print(name!)
    self.textfieldName.text = name!


    for number in contact.phoneNumbers {

        print("number ----\(number)")

        let mobile = number.value.value(forKey: "digits") as? String
        if (mobile?.count)! > 7 {
            // your code goes here
            print("mobile---\(String(describing: mobile))")
            self.textfieldMobileNumber.text = mobile!
        }
    }


//        print(contact.emailAddresses[0].value.value(forKey: "labelValuePair") as! String)

    for email in contact.emailAddresses {

        print("number ----\(email)")

        let eml = email.value(forKey: "value") as? String
        print("eml --\(eml!)")
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!