I followed Apple Address Book Programming Guide for iOS to get phonenumber from addressbook to my UITextField. But I have two UITextField and doesn’t know how to implement same function to second textfield.
Presenting the people picker
- (IBAction)showPicker:(id)sender
{
ABPeoplePickerNavigationController *picker =
[[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentModalViewController:picker animated:YES];
}
Responding to user actions in the people picker
- (void)peoplePickerNavigationControllerDidCancel:
(ABPeoplePickerNavigationController *)peoplePicker
{
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
[self displayPerson:person];
[self dismissModalViewControllerAnimated:YES];
return NO;
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
return NO;
}
Displaying a person’s information
- (void)displayPerson:(ABRecordRef)person
{
NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person,
kABPersonFirstNameProperty);
self.firstName.text = name;
NSString* phone = nil;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,
kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
} else {
phone = @"[None]";
}
self.phoneNumber.text = phone;
}
EDIT
- (void)displayPerson:(ABRecordRef)person
{
NSString* phone = nil;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
} else {
phone = @"None";
}
self.phoneNumber.text = phone;
if (ABMultiValueGetCount(phoneNumbers) > 1) {
phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 1);
} else {
phone = @"None";
}
self.phoneNumber2.text = phone;
}

I assume your second
UITextFieldis for a second phone number, if it exists? You could then do:Or you can use
CFBridgingReleaseinstead of__bridge_transfer:And, by the way, wouldn’t you also want to use
ABMultiValueCopyLabelAtIndexto capture what type each phone number was (it doesn’t help to know what a phone number is if you don’t know what type of number it is). You could store these two phone number labels in two additional UITextFields (presumably next to their respective phone numbers).Update:
I originally thought that you were looking for two phone numbers from the same contact. I’m now inferring from your user interface, where you have a “+” button next to each of the two phone number text fields that you want to tap on the button, and pull up the phone number for that text field. And then you’d then tap on the “+” button next to the other text field, and then pull up the phone number for a different contact and put it in that text field.
So, if this is the case, you might add an ivar to your class, such as:
And then the first “+” button would have an
IBActionlike:Whereas the second “+” button would have an
IBActionlike:Then your
displayPersonwould be something like:By the way, while I think this does what you’re looking for, it’s not clear what you should be doing with the first name field (because you’re picking two contacts, which first name do you want to use, or will you store the two first names in two fields, just like you do with phone numbers?). So, you’ll have to look at that.
Clearly, if you didn’t want two
IBActionmethods for the two “+” buttons, you could probably do a single one that looked atsenderor it’s tag, but that’s not material to your question. Bottom line, use an ivar to designate which phone number you want to update.