【发布时间】:2017-01-30 09:36:12
【问题描述】:
我正在尝试使用更现代的CNContactPickerViewController 来选择联系人。如果联系人有多个地址,我希望用户只能选择其中一个地址。如果专门选择了地址,我也想获取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)
所以,对我来说,新的联系人框架中似乎不再提供相同的功能,但我在这里查看是否遗漏了什么。
【问题讨论】:
-
我的错误是在 CNPostalAddress 中查找“联系人”属性。而不是查看contactProperty。
标签: ios swift contacts-framework