1、两个框架:AddressBook.framework和AddressBookUI.framework,使用这两个框架我们可以在程序中访问并显示iPhone数据库中的联系人信息。
使用时引入头文件 #import<AddressBook/AddressBook.h> 和 #import<AddressBookUI/AddressBookUI.h>
可以通过实现ABPeoplePickerNavigationControllerDelegate的方法来访问通讯录,也可以通过ABAddressBookRef(获取通讯录对象,可以遍历它来获取通讯录中所有的人)和ABRecordRef(通讯录中一个人的对象,可以通过这个对象来获取所有用户的个人信息。)
2、通过ABPeoplePickerNavigationControllerDelegate方法来访问通讯录,主要是以下三个方法
(a)peoplePickerNavigationControllerDidCancel:当用户选择取消时调用这个方法,可以在这个方法里取消整个通讯录页面的显示。
(b)peoplePickerNavigationController:shouldContinueAfterSelectingPerson: 当用户选择了通讯录中某一个联系人时调用这个方法,可以在这里获取联系人的信息。如果希望可以继续显示这个联系人更具体的信息,则return YES。否则取消整个通讯录页面的显示并return NO。
(c)peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier: 如果上一个方法返回的是YES,则会显示某一个联系人信息,如果选择了联系人的某一项纪录,就会调用这个方法,可以通过点击选择联系人的某一项信息。如果 希望可以对选择的某一项纪录进行进一步操作,比如直接拨打电话或调用邮箱发送邮件,则return YES。否则取消整个通讯录页面的显示并return NO。
if(!picker){ picker = [[ABPeoplePickerNavigationController alloc] init]; // place the delegate of the picker to the controll NSArray *displayedItems = [NSArray arrayWithObject: [NSNumber numberWithInt:kABPersonPhoneProperty]]; picker.displayedProperties = displayedItems; picker.peoplePickerDelegate = self; } // showing the picker [self presentViewController:picker animated:YES completion:nil];
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { return YES; } - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { if (property == kABPersonPhoneProperty) { ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, property); int index = ABMultiValueGetIndexForIdentifier(phoneMulti,identifier); NSString *phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneMulti, index); //do something self.fieldMobileNo.text = phone; [peoplePicker dismissViewControllerAnimated:YES completion:nil]; } return NO; } - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker { // assigning control back to the main controller [picker dismissViewControllerAnimated:YES completion:nil]; }
NSArray *displayedItems = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]]; picker.displayedProperties = displayedItems;上面代码可以控制选择联系人之后显示的字段列表,上面只显示手机号,也可以加入
NSArray *displayedItems = [NSArray arrayWithObjects: [NSNumber numberWithInt:kABPersonPhoneProperty], [NSNumber numberWithInt:kABPersonEmailProperty], [NSNumber numberWithInt:kABPersonBirthdayProperty], nil];
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { NSArray *phones = [AddressBookUtil getPhones:person]; //一些处理... UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"手机号码" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; sheet.actionSheetStyle = UIActionSheetStyleAutomatic; for(NSString *pho in phones) { [sheet addButtonWithTitle:pho]; } [sheet addButtonWithTitle:@"取消"]; sheet.cancelButtonIndex = phones.count; if(phones.count>0){ [sheet showInView:[UIApplication sharedApplication].keyWindow]; }else{ [peoplePicker dismissModalViewControllerAnimated:YES]; } return NO; }
参考以下网址:
http://rdc.taobao.org/?author=49
http://www.cnblogs.com/y041039/archive/2012/03/22/2411771.html