如何使用 CNContactPickerViewController 获取地址和联系人?
How to use CNContactPickerViewController to get address AND contact?
我正在尝试使用更现代的 CNContactPickerViewController
来 select 联系方式。如果联系人有多个地址,我希望用户能够 select 只有一个地址。如果地址是特定的 selected,我也想获得 CNContact
对象。
我可以使用已弃用的 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: "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 中似乎不再提供相同的功能,但我正在检查这里是否遗漏了什么。
Note that there is no CNContact
object returned. I get the
CNPostalAddress
in the contactProperty, but I don't receive the
record of the owning contact.
CNContact
对象可以从CNContactProperty
的contact
属性中获取,所以...
However, when using CNContactPickerViewController
, only this
relevant delegate function is available:
func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty)
…实现这个委托方法可以让你做你想做的事。但是你想确保你没有实现其他委托方法,例如:
func contactPicker(CNContactPickerViewController, didSelect: CNContact)
这将在选择联系人(而不是其 属性)时关闭选择器。
Note that there is no CNContact object
你完全错了。委托方法是 contactPicker(_:didSelect:)
,它给你一个 CNContactProperty,而 CNContactProperty 给你整个 CNContact 及其所有属性。
我正在尝试使用更现代的 CNContactPickerViewController
来 select 联系方式。如果联系人有多个地址,我希望用户能够 select 只有一个地址。如果地址是特定的 selected,我也想获得 CNContact
对象。
我可以使用已弃用的 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: "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 中似乎不再提供相同的功能,但我正在检查这里是否遗漏了什么。
Note that there is no
CNContact
object returned. I get theCNPostalAddress
in the contactProperty, but I don't receive the record of the owning contact.
CNContact
对象可以从CNContactProperty
的contact
属性中获取,所以...
However, when using
CNContactPickerViewController
, only this relevant delegate function is available:func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty)
…实现这个委托方法可以让你做你想做的事。但是你想确保你没有实现其他委托方法,例如:
func contactPicker(CNContactPickerViewController, didSelect: CNContact)
这将在选择联系人(而不是其 属性)时关闭选择器。
Note that there is no CNContact object
你完全错了。委托方法是 contactPicker(_:didSelect:)
,它给你一个 CNContactProperty,而 CNContactProperty 给你整个 CNContact 及其所有属性。