我正在尝试使用更现代CNContactPickerViewController
的方式来选择联系人。如果联系人有多个地址,我希望用户只能选择其中一个地址。如果专门选择了一个地址,我也想获取该CNContact
对象。
我可以使用 deprecated 来做到这一点ABPeoplePickerNavigationControllerDelegate
,该委托功能可用:
func peoplePickerNavigationController(_ peoplePicker: ABPeoplePickerNavigationController, didSelectPerson person: ABRecord, property: ABPropertyID, identifier: ABMultiValueIdentifier)
但是,当使用 时CNContactPickerViewController
,只有这个相关的委托函数可用:
func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty)
请注意,没有CNContact
返回对象。我CNPostalAddress
在contactProperty中得到了,但我没有收到拥有联系人的记录。
这是我使用的代码ABPeoplePickerNavigationController
:
let peoplePicker = ABPeoplePickerNavigationController()
peoplePicker.peoplePickerDelegate = self
peoplePicker.displayedProperties = [NSNumber(value: kABPersonAddressProperty as Int32), NSNumber(value: kABPersonBirthdayProperty as Int32)]
peoplePicker.predicateForSelectionOfPerson = NSPredicate(format: "postalAddresses.@count <= 1")
peoplePicker.modalPresentationStyle = UIModalPresentationStyle.currentContext
self.present(peoplePicker, animated: true, completion: nil)
...这是 CNContactPickerViewController 的代码:
let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
contactPicker.displayedPropertyKeys = [CNContactPostalAddressesKey, CNContactBirthdayKey]
contactPicker.predicateForSelectionOfContact = NSPredicate(format: "postalAddresses.@count <= 1")
contactPicker.modalPresentationStyle = UIModalPresentationStyle.currentContext
self.present(contactPicker, animated: true, completion: nil)
所以,对我来说,在较新的 Contacts Framework 中似乎不再提供相同的功能,但我在这里查看是否遗漏了什么。