iOS10上的地址簿崩溃

时间:2016-09-23 00:38:26

标签: addressbook ios10 abaddressbook

从联系人选择器中选择联系人会崩溃iOS10.0中的应用程序。联系人选择器使用ABPeoplePickerNavigationController显示,如下所示:

let contactsPicker = ABPeoplePickerNavigationController()
contactsPicker.peoplePickerDelegate = self
self.presentViewController(contactsPicker, animated: true, completion: nil)

以下是崩溃日志中的堆栈跟踪:

*** Terminating app due to uncaught exception 'CNPropertyNotFetchedException', reason: 'A property was not requested when contact was fetched.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000105a1c34b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00000001052cd21e objc_exception_throw + 48
    2   CoreFoundation                      0x0000000105a85265 +[NSException raise:format:] + 197
    3   Contacts                            0x000000010dc6d96f -[CNContact sectionForSortingByFamilyName] + 160
    4   Contacts                            0x000000010dc3e18e __55-[CNContact(iOSABCompatibility) overwritePerson:error:]_block_invoke + 44
    5   CoreFoundation                      0x00000001059ad2fd __53-[__NSArrayI enumerateObjectsWithOptions:usingBlock:]_block_invoke + 77
    6   CoreFoundation                      0x00000001059ad1df -[__NSArrayI enumerateObjectsWithOptions:usingBlock:] + 207
    7   Contacts                            0x000000010dc3e0f4 -[CNContact(iOSABCompatibility) overwritePerson:error:] + 240
    8   Contacts                            0x000000010dc3dfc0 -[CNContact(iOSABCompatibility) detachedPersonWithError:] + 46
    9   AddressBookUI                       0x00000001057bdd77 -[ABPeoplePickerNavigationController contactPicker:didSelectContact:] + 145
    10  ContactsUI                          0x0000000112396eb2 -[CNContactPickerViewController pickerDidSelectContact:property:] + 306
    11  ContactsUI                          0x000000011243ee6f -[CNContactPickerHostViewController pickerDidSelectContact:property:] + 95
    12  ContactsUI                          0x000000011243f5ec __71-[CNContactPickerExtensionHostContext pickerDidSelectContact:property:]_block_invoke + 66

我已在Contact Address book crash on iOS 10 beta中讨论的info.plist中添加了NSContactsUsageDescription,但这没有帮助,我无法使用CNContactPickerViewController,因为我需要支持iOS8设备。

3 个答案:

答案 0 :(得分:1)

Imran Raheem

来自Erdekhayser的解决方案(Contact Address book crash on iOS 10 beta

您可以使用此方法检查CNContactPickerViewController是否可用?

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];
    }

答案 1 :(得分:0)

在iOS 9中不推荐使用Address Book API,而是支持更面向对象的Contacts Framework。

而不是使用ABPeoplePickerViewController,转移到CNContactPickerViewController。

答案 2 :(得分:0)

当我尝试从委托方法emailAddresses中获取CNContact时,我遇到了同样的错误。

最初,我初始化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)
}

代理方法:

// MAKE:联系代表

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)

}

我在未声明初始化的情况下访问了电子邮件地址。 错误-Terminating app due to uncaught exception 'CNPropertyNotFetchedException', reason: 'A property was not requested when contact was fetched.'

用于访问电子邮件的正确代码---

Xcode 10。和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!)")
    }
}