我想用更现代的CNContactPickerViewController
来选择一个联系人。如果联系人有多个地址,我希望用户只能选择其中一个地址。如果地址是专门选择的,我也想获得CNContact
对象。如何使用CNContactPickerViewController获取地址和联系人?
我可以使用过时的ABPeoplePickerNavigationControllerDelegate
,在此委托功能可用做:
func peoplePickerNavigationController(_ peoplePicker: ABPeoplePickerNavigationController, didSelectPerson person: ABRecord, property: ABPropertyID, identifier: ABMultiValueIdentifier)
然而,在使用CNContactPickerViewController
时,仅此相关的委托功能可用:
func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty)
注意没有CNContact
对象返回。我在contactProperty中获得CNPostalAddress
,但我没有收到拥有联系人的记录。
这是我与ABPeoplePickerNavigationController
使用的代码:
let peoplePicker = ABPeoplePickerNavigationController()
peoplePicker.peoplePickerDelegate = self
peoplePicker.displayedProperties = [NSNumber(value: kABPersonAddressProperty as Int32), NSNumber(value: kABPersonBirthdayProperty as Int32)]
peoplePicker.predicateForSelectionOfPerson = NSPredicate(format: "[email protected] <= 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: "[email protected]unt <= 1")
contactPicker.modalPresentationStyle = UIModalPresentationStyle.currentContext
self.present(contactPicker, animated: true, completion: nil)
所以,对我来说,它看起来像相同的功能不再在较新的联系人框架中可用,但我在这里检查是否错过了某些内容。
我的错误是查看CNPostalAddress中的“联系人”属性。而不是看contactProperty。 – Daniel