【问题标题】:How to use CNContactPickerViewController to get address AND contact?如何使用 CNContactPickerViewController 获取地址和联系方式?
【发布时间】: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


【解决方案1】:

请注意,没有返回 CNContact 对象。我明白了 CNPostalAddress 在contactProperty 中,但我没有收到 拥有联系人的记录。

CNContact 对象可以从CNContactPropertycontact 属性中检索,所以……

但是,当使用CNContactPickerViewController 时,只有这个 相关委托功能可用:

func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty)

...实现这个委托方法可以让你做你想做的事。但您要确保您没有实现其他委托方法,例如:

func contactPicker(CNContactPickerViewController, didSelect: CNContact)

这将在选择联系人(而不是其属性)时关闭选择器。

【讨论】:

    【解决方案2】:

    注意这里没有 CNContact 对象

    你错了。委托方法是contactPicker(_:didSelect:),它给你一个 CNContactProperty,而 CNContactProperty 给你整个 CNContact 及其所有属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-16
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      相关资源
      最近更新 更多