3

我正在使用 CNContactPickerViewController 让用户选择他/她的联系人之一的电子邮件

let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
contactPicker.predicateForSelectionOfContact = NSPredicate(format: "emailAddresses.@count > 0")
contactPicker.displayedPropertyKeys = [CNContactNicknameKey, CNContactEmailAddressesKey]

当联系人只有一封邮件时,一切顺利

func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {

    if let _mail = contact.emailAddresses.first?.value as String? {
        self.personWasSelected(with: _mail)
    }
}

但有时其中一个联系人有不止一封邮件,我怎样才能让用户选择其中之一?

4

2 回答 2

4

要让用户选择要选择的电子邮件地址,请设置

picker.predicateForEnablingContact = NSPredicate(format: "emailAddresses.@count > 0")
picker.predicateForSelectionOfContact = NSPredicate(format: "emailAddresses.@count == 1")

这样,对于只有一封电子邮件的用户

contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact)

被调用,但对于拥有多于一封电子邮件的用户,会显示联系人详细信息并将所选电子邮件传递给

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

注意CNContactProperty也有.contact属性。

于 2018-11-17T18:47:35.247 回答
3

好的,通过试错找到了解决方案...

我正在实施这两个:

func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty) {
    if let _mail = contactProperty.value as? String {
        self.personWasSelected(with: _mail)
    }
}

func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {

    if let _mail = contact.emailAddresses.first?.value as String? {
        self.personWasSelected(with: _mail)
    }
}

但只有第一个应该已经实施

于 2018-06-04T15:17:25.760 回答