I want to have my user select a phone number for someone in their contacts and I want to return that number to the app so that I can use it for later. Here is my code for allowing the user to select one of their contacts phone numbers, but when I click a phone number it calls that number (which I don't want to happen) and doesn't return anything to my app.
class ViewController: UIViewController {
var people = ABPeoplePickerNavigationController()
var addressBook: ABAddressBookRef?
func extractABAddressBookRef(abRef: Unmanaged<ABAddressBookRef>!) -> ABAddressBookRef? {
if let ab = abRef {
self.view.addSubview(people.view)
return Unmanaged<NSObject>.fromOpaque(ab.toOpaque()).takeUnretainedValue()
}
return nil
}
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecordRef!, property: ABPropertyID, identifier: ABMultiValueIdentifier) {
let multiValue: ABMultiValueRef = ABRecordCopyValue(person, property).takeRetainedValue()
let index = ABMultiValueGetIndexForIdentifier(multiValue, identifier)
let phoneNumber = ABMultiValueCopyValueAtIndex(multiValue, index).takeRetainedValue() as! String
println(phoneNumber)
}
I have implemented a solution that only show contacts that have phone numbers, and the user has to select a concrete phone number. Your ViewController has to implement
ABPeoplePickerNavigationControllerDelegateprotocol.Here is my code: