如何使用CNContactPickerViewController获取地址和联系?
我正在尝试使用更现代的CNContactPickerViewController
来选择联系人。 如果联系人有多个地址,我希望用户只能选择其中一个地址。 如果专门选择了一个地址,我也想获得CNContact
对象。
我可以使用已弃用的ABPeoplePickerNavigationControllerDelegate
执行此操作,此委托函数可用:
func peoplePickerNavigationController(_ peoplePicker: ABPeoplePickerNavigationController, didSelectPerson person: ABRecord, property: ABPropertyID, identifier: ABMultiValueIdentifier)
但是,使用CNContactPickerViewController
,只有相关的委托function可用:
func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty)
请注意,没有返回CNContact
对象。 我在CNPostalAddress
中获取了CNPostalAddress
,但是我没有收到拥有联系人的记录。
这是我与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)
所以,对我来说,看起来新的联系人框架中不再提供相同的function,但我在这里查看是否遗漏了一些内容。
请注意,没有返回
CNContact
对象。 我在CNPostalAddress
中获取了CNPostalAddress
,但是我没有收到拥有联系人的记录。
可以从CNContact
的contact
属性中检索CNContact
对象,所以……
但是,使用
CNContactPickerViewController
,只有相关的委托function可用:func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty)
…实现这种委托方法可以让你做你想做的事。 但是你想确保你没有实现其他委托方法,例如:
func contactPicker(CNContactPickerViewController, didSelect: CNContact)
这将在选择联系人(而不是其财产)时解雇选择器。
请注意,没有CNContact对象
你错了。 委托方法是contactPicker(_:didSelect:)
,它向您发送CNContactProperty,CNContactProperty向您提供整个CNContact及其所有属性。