我想用更現代的CNContactPickerViewController
來選擇一個聯繫人。如果聯繫人有多個地址,我希望用戶只能選擇其中一個地址。如果地址是專門選擇的,我也想獲得CNContact
對象。如何使用CNContactPickerViewController獲取地址和聯繫人?
我可以使用過時的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: "[email protected] <= 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: "[email protected]unt <= 1")
contactPicker.modalPresentationStyle = UIModalPresentationStyle.currentContext
self.present(contactPicker, animated: true, completion: nil)
所以,對我來說,它看起來像相同的功能不再在較新的聯繫人框架中可用,但我在這裏檢查是否錯過了某些內容。
我的錯誤是查看CNPostalAddress中的「聯繫人」屬性。而不是看contactProperty。 – Daniel