【问题标题】:Address Book crash on iOS10iOS10通讯录崩溃
【发布时间】:2017-01-31 17:31:00
【问题描述】:

在 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

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

【问题讨论】:

标签: addressbook ios10 abaddressbook


【解决方案1】:

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

一开始,我初始化了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!)")
    }
}

【讨论】:

    【解决方案2】:

    伊姆兰·拉希姆

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

    【讨论】:

      【解决方案3】:

      地址簿 API 在 iOS 9 中被弃用,取而代之的是更加面向对象的联系人框架。

      不要使用 ABPeoplePickerViewController,而是使用 CNContactPickerViewController。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多