2016-09-22 177 views
1

我想用更現代的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) 

所以,對我來說,它看起來像相同的功能不再在較新的聯繫人框架中可用,但我在這裏檢查是否錯過了某些內容。

+0

我的錯誤是查看CNPostalAddress中的「聯繫人」屬性。而不是看contactProperty。 – Daniel

回答

3

請注意,沒有CNContact對象返回。我在contactProperty中獲得 CNPostalAddress,但我沒有收到擁有聯繫人的 記錄。

CNContact對象可以從CNContactPropertycontact屬性檢索,所以......

然而,在使用CNContactPickerViewController時,僅此 相關的委託功能可用:

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

...實現這個委託方法可以讓你做什麼y你想要。但是你要確保你實現其他委託方法,如:

func contactPicker(CNContactPickerViewController, didSelect: CNContact) 

這將解僱選擇器的接觸(而不是其財產)被選中的那一刻。

1

注意,沒有CNContact對象

你只是錯了。委託方法是contactPicker(_:didSelect:),它將交付給您一個CNContactProperty,並且CNContactProperty將整個CNContact及其所有屬性交給您。

相關問題