首先,导入AddressBook.framework和AddressBookUI.framework。
addressBookViewController.h
#import <UIKit/UIKit.h> #import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> @interface addressBookViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate> { UIButton *button; IBOutlet UILabel *firstName; IBOutlet UILabel *lastName; IBOutlet UILabel *number; } - (IBAction)getContact; @end
addressBookViewController.m
#import "addressBookViewController.h" @implementation addressBookViewController - (IBAction)getContact { ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init]; picker.peoplePickerDelegate = self; [self presentModalViewController:picker animated:YES]; [picker release]; } - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker { [self dismissModalViewControllerAnimated:YES]; } - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { firstName.text = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); lastName.text = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty); number.text = (NSString *)ABMultiValueCopyValueAtIndex(multi, 0); [self dismissModalViewControllerAnimated:YES]; return NO; } - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{ return NO; } - (void)dealloc { [super dealloc]; } @end