Here’s an updated version of this apple developer walkthrough of how to programmatically access the iPhone address book:
Note: only runs with physically connected iPhone – hence no screen shots.
The address book
The way to the iPhone address book is through the ABPeoplePickerNavigationControllerDelegate.
Yes you will need to import:
#import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h>
as described here.
When the user clicks the Tap me! button, we throw up the people picker navigation controller:
- (IBAction)readAddressBook:(id)sender { ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init]; picker.peoplePickerDelegate = self; [self presentModalViewController:picker animated:YES]; }
Which then calls us later via the people picker callback:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { NSString* name = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); self.firstName.text = name; [self dismissModalViewControllerAnimated:YES]; return NO; }
Because we are done once we get the persons name and update the label we then dismiss the controller:
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{ return NO; }
Here’s the code in all it’s glory:
ViewController.m
#import "Spike3ViewController.h" #import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> @interface Spike3ViewController() <ABPeoplePickerNavigationControllerDelegate> @property (weak, nonatomic) IBOutlet UIButton *button; @property (weak, nonatomic) IBOutlet UILabel *firstName; @end @implementation Spike3ViewController @synthesize button = _button; @synthesize firstName = _firstName; - (IBAction)readAddressBook:(id)sender { ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init]; picker.peoplePickerDelegate = self; [self presentModalViewController:picker animated:YES]; } - (void)peoplePickerNavigationControllerDidCancel: (ABPeoplePickerNavigationController *)peoplePicker { [self dismissModalViewControllerAnimated:YES]; } - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { NSString* name = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); self.firstName.text = name; [self dismissModalViewControllerAnimated:YES]; return NO; } - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{ return NO; } - (void)viewDidUnload { [self setButton:nil]; [self setFirstName:nil]; [super viewDidUnload]; } @end
Feb 03, 2012 @ 04:52:33
nice post
Re your images: to take a screenshot on ios devices: press and hold home button then click power button. screenshot is stored in photos app (or iCloud)
Feb 03, 2012 @ 13:25:23
As yes I didn’t think of that. Thank you anonymous. Cheers.
Dec 28, 2013 @ 23:32:51
Hi! How can I use this code with kABPersonPhoneProperty? In other words: To get the phone number from a contact?
Thanks! 🙂
Aug 13, 2014 @ 15:30:54
You can get the phone number Like this
– (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
NSString* name = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
NSLog(@"phone:%@", phoneNumber);
}
[self dismissModalViewControllerAnimated:YES];
return NO;
}
Apr 09, 2015 @ 11:04:37
– (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker is not getting called