调用通讯录之前 先应导入两个工具包 AddressBook.framework 和AddressBookUI.framework;
复制代码
复制代码
- #import <UIKit/UIKit.h>
- #import <AddressBook/AddressBook.h>
- #import <AddressBookUI/AddressBookUI.h>
- @interface YourClass : UIViewController<ABPeoplePickerNavigationControllerDelegate>
- @end
- #import <YourClass.h>
- @implementation YourClass
- -(void)onClickButtonUIButton *)sender{
- 进入通讯录的事件
- ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
- peoplePicker.peoplePickerDelegate = self;
- [self.navigationController presentModalViewController:peoplePicker animated:YES ];
- }
- - (BOOL)peoplePickerNavigationControllerABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPersonABRecordRef)person
- {
- ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);
- ABMutableMultiValueRef address=ABRecordCopyValue(person, kABPersonAddressProperty);
- ABMutableMultiValueRef birthday=ABRecordCopyValue(person, kABPersonBirthdayProperty);
- ABMutableMultiValueRef creationDate=ABRecordCopyValue(person, kABPersonCreationDateProperty);
- ABMutableMultiValueRef date=ABRecordCopyValue(person, kABPersonDateProperty);
- ABMutableMultiValueRef department=ABRecordCopyValue(person, kABPersonDepartmentProperty);
- ABMutableMultiValueRef email=ABRecordCopyValue(person, kABPersonEmailProperty);
- ABMutableMultiValueRef firstNamePhonetic=ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty);
- ABMutableMultiValueRef firstName=ABRecordCopyValue(person, kABPersonFirstNameProperty);
- ABMutableMultiValueRef instantMessage=ABRecordCopyValue(person, kABPersonInstantMessageProperty);
- ABMutableMultiValueRef jobTitle=ABRecordCopyValue(person, kABPersonJobTitleProperty);
- ABMutableMultiValueRef kind=ABRecordCopyValue(person, kABPersonKindProperty);
- ABMutableMultiValueRef lastNamePhonetic=ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty);
- ABMutableMultiValueRef lastName=ABRecordCopyValue(person, kABPersonLastNameProperty);
- ABMutableMultiValueRef middleNamePhonetic=ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty);
- ABMutableMultiValueRef middleName=ABRecordCopyValue(person, kABPersonMiddleNameProperty);
- ABMutableMultiValueRef modificationDate=ABRecordCopyValue(person, kABPersonModificationDateProperty);
- ABMutableMultiValueRef nickname=ABRecordCopyValue(person, kABPersonNicknameProperty);
- ABMutableMultiValueRef note=ABRecordCopyValue(person, kABPersonNoteProperty);
- ABMutableMultiValueRef organization=ABRecordCopyValue(person, kABPersonOrganizationProperty);
- ABMutableMultiValueRef phone=ABRecordCopyValue(person, kABPersonPhoneProperty);
- ABMutableMultiValueRef prefix=ABRecordCopyValue(person, kABPersonPrefixProperty);
- ABMutableMultiValueRef relatedNames=ABRecordCopyValue(person, kABPersonRelatedNamesProperty);
- ABMutableMultiValueRef socialProfile=ABRecordCopyValue(person, kABPersonSocialProfileProperty);
- ABMutableMultiValueRef personSuffix=ABRecordCopyValue(person, kABPersonSuffixProperty);
- ABMutableMultiValueRef _URL=ABRecordCopyValue(person, kABPersonURLProperty);
- NSMutableArray *phones = [[NSMutableArray alloc] init];
- // int i;
- for (int i = 0; i < ABMultiValueGetCount(phoneMulti); i++) {
- NSString *aPhone = [(NSString*)ABMultiValueCopyValueAtIndex(phoneMulti, i) autorelease];
- [phones addObject:aPhone];
- }
- NSLog(@"+qqqqqqqqqq++++%@",phones);
- NSLog(@"****************");
- NSString *mobileNo = [phones objectAtIndex:0];
- // self.label.text = (NSString*)ABRecordCopyCompositeName(person);
- NSLog(@"++++++++++++++++++++%@",mobileNo);
- [self dismissModalViewControllerAnimated:YES];
- [peoplePicker release];
- return NO;
- }
- - (BOOL)peoplePickerNavigationControllerABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPersonABRecordRef)person propertyABPropertyID)property identifierABMultiValueIdentifier)identifier
- {
- [self dismissModalViewControllerAnimated:YES];
- [peoplePicker release];
- return NO;
- }
- - (void)peoplePickerNavigationControllerDidCancelABPeoplePickerNavigationController *)peoplePicker
- {
- [self dismissModalViewControllerAnimated:YES];
- [peoplePicker release];
- }