Reputation: 693
I have an application that has some fields for a person's name, email, and address. I have utilized the CNContactPickerViewController and have the delegate stated in my application header. On a button click, I have the following:
@IBAction func AddItemFromContactsPressed(sender: AnyObject) {
let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
contactPicker.displayedPropertyKeys =
[CNContactEmailAddressesKey, CNContactPhoneNumbersKey]
self.presentViewController(contactPicker, animated: true, completion: nil)
}
afterwards, when the user selects a contact, I have the delegate method didSelectContact and have utilized it as follows:
//DELEGATE METHODS FOR UICONTACT
func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact){
ItemName.text = contact.givenName + " " + contact.familyName
if(contact.phoneNumbers.count != 0){
ItemPhoneContact.text = contact.phoneNumbers[0].value as? String
}
if(contact.postalAddresses.count != 0){
ItemAddress.text = contact.postalAddresses[0].value as? String
}
}
However I get a runtime error on pulling the postalAddresses. When commneted out, I get the following warning on runtime for getting the phone numbers:
[Appname ####:####] plugin com.apple.MobileAddressBook.ContactsViewService invalidated
Any ideas as to what is going on? Thanks in advance!
EDIT: I would just like to note that getting the name and family name works perfectly fine.
Upvotes: 0
Views: 1337
Reputation: 1
You can use CNContactViewController to show contact details based on name.
let name = "\(contact.firstName) \(contact.lastName)"
let predicate: NSPredicate = CNContact.predicateForContacts(matchingName: name)
let descriptor = CNContactViewController.descriptorForRequiredKeys()
let contacts: [CNContact]
do {
contacts = try store.unifiedContacts(matching: predicate, keysToFetch: [descriptor])
} catch {
contacts = []
}
if !contacts.isEmpty {
guard let contact.first else {return}
let cvc = CNContactViewController(for: contact)
cvc.allowsActions = true
cvc.delegate = self
cvc.allowsEditing = true
self.navigationController?.pushViewController(cvc, animated: true)
}
Upvotes: 0
Reputation: 1321
You are seeing this message because in IOS 9 ABPeoplePickerNavigationController is no longer works.
There is new library/framework for CNContact which will be used in IOS 9 and above: https://developer.apple.com/library/prerelease/ios/documentation/Contacts/Reference/Contacts_Framework/index.html
Upvotes: 1