你可以使用这个 - 虽然,如果联系人有多个电话号码,这只会得到第一个......
var thePhoneLabel: String?
var thePhoneNumber: String?
func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact) {
picker.dismissViewControllerAnimated(true, completion: {
if contact.phoneNumbers.count > 0 {
if let anEntry = contact.phoneNumbers.first {
if let theNumber = anEntry.value as? CNPhoneNumber {
// Get the label for the phone number (Home, Work, Mobile, etc)
self.thePhoneLabel = CNLabeledValue.localizedStringForLabel(anEntry.label)
// Get the actual phone number (as a string)
self.thePhoneNumber = theNumber.stringValue
}
}
} else {
// contact has no phone numbers
self.thePhoneLabel = "(No Phone)"
self.thePhoneNumber = "(No Phone)"
}
})
}
编辑:
如果使用:
contactPickerViewController.displayedPropertyKeys = [CNContactPhoneNumbersKey]
然后:
var theContactName: String?
var thePhoneNumber: String?
var thePhoneLabel: String?
func contactPicker(picker: CNContactPickerViewController, didSelectContactProperty contactProperty: CNContactProperty) {
theContactName = contactProperty.contact.givenName
thePhoneNumber = contactProperty.value?.stringValue
if let lbl = contactProperty.label {
thePhoneLabel = CNLabeledValue.localizedStringForLabel(lbl)
}
}
笔记:
func contactPicker(_picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty)
仅当您没有
func contactPicker(_picker: CNContactPickerViewController, didSelect 联系人: CNContact)