Swift - 从 ContactPickerViewController 获取联系人详细信息
Swift - Getting Contact Details from ContactPickerViewController
我有一个应用程序,其中包含个人姓名、电子邮件和地址的一些字段。我已经使用了 CNContactPickerViewController 并在我的申请 header 中说明了委托人。单击按钮,我有以下内容:
@IBAction func AddItemFromContactsPressed(sender: AnyObject) {
let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
contactPicker.displayedPropertyKeys =
[CNContactEmailAddressesKey, CNContactPhoneNumbersKey]
self.presentViewController(contactPicker, animated: true, completion: nil)
}
之后,当用户选择联系人时,我有委托方法 didSelectContact 并按如下方式使用它:
//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
}
}
但是,我在拉取 postalAddresses 时遇到运行时错误。当 commneted 时,我在运行时收到以下警告以获取 phone 数字:
[Appname ####:####] plugin com.apple.MobileAddressBook.ContactsViewService invalidated
对发生的事情有什么想法吗?
提前致谢!
编辑:我只想指出,获取名字和姓氏非常好。
您看到此消息是因为在 IOS 9 ABPeoplePickerNavigationController 中不再有效。
CNContact 有新的 library/framework,将在 IOS 9 及更高版本中使用:https://developer.apple.com/library/prerelease/ios/documentation/Contacts/Reference/Contacts_Framework/index.html
您可以使用 CNContactViewController 根据姓名显示联系人详细信息。
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)
}
我有一个应用程序,其中包含个人姓名、电子邮件和地址的一些字段。我已经使用了 CNContactPickerViewController 并在我的申请 header 中说明了委托人。单击按钮,我有以下内容:
@IBAction func AddItemFromContactsPressed(sender: AnyObject) {
let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
contactPicker.displayedPropertyKeys =
[CNContactEmailAddressesKey, CNContactPhoneNumbersKey]
self.presentViewController(contactPicker, animated: true, completion: nil)
}
之后,当用户选择联系人时,我有委托方法 didSelectContact 并按如下方式使用它:
//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
}
}
但是,我在拉取 postalAddresses 时遇到运行时错误。当 commneted 时,我在运行时收到以下警告以获取 phone 数字:
[Appname ####:####] plugin com.apple.MobileAddressBook.ContactsViewService invalidated
对发生的事情有什么想法吗? 提前致谢!
编辑:我只想指出,获取名字和姓氏非常好。
您看到此消息是因为在 IOS 9 ABPeoplePickerNavigationController 中不再有效。
CNContact 有新的 library/framework,将在 IOS 9 及更高版本中使用:https://developer.apple.com/library/prerelease/ios/documentation/Contacts/Reference/Contacts_Framework/index.html
您可以使用 CNContactViewController 根据姓名显示联系人详细信息。
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)
}