通讯录中联系人相关的应用iPhone提供了两个框架:AddressBook.framework和AddressBookUI.framework,
使用这两个框架我们可以在程序中访问并显示iPhone数据库中的联系人信息。
1.AddressBookUI显示部分
AddressBookUI中提供了和联系人显示信息相关的一些Controller,有四个:
ABPeoplePickerNavigationController:显示整个通讯录并可以选择一个联系人的信息
ABPersonViewController:显示一个具体联系人的信息
ABNewPersonViewController:增加一个新的联系人
ABUnknownPersonViewController:完善一个联系人的信息
由于其中最主要的是ABPeoplePickerNavigationController,因此就具体的介绍一下通过程序显示整个通讯录并且可以选择其中某个联系人信息的步骤。
(a)创建并初始化一个ABPeoplePickerNavigationController对象
(b)设置其代理(delegate)
(c)用presentModalViewController:animated:这个方法进行显示整个通讯录页面
例子:
- - (IBAction)showPicker:(id)sender {
- ABPeoplePickerNavigationController *picker =
- [[ABPeoplePickerNavigationController alloc] init];
- picker.peoplePickerDelegate = self;
- [self presentModalViewController:picker animated:YES];
- [picker release];
- }
ABPeoplePickerNavigationControllerDelegate的方法介绍
(1)
- peoplePickerNavigationControllerDidCancel:
当用户选择取消时调用这个方法,可以在这个方法里取消整个通讯录页面的显示。
(2)
- peoplePickerNavigationController:shouldContinueAfterSelectingPerson:
当用户选择了通讯录中某一个联系人时调用这个方法,可以在这里获取联系人的信息。如果希望可以继续显示这个联系人更具体的信息,则return YES。否则取消整个通讯录页面的显示并return NO。
(3)
- eoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier:
如果上一个方法返回的是YES,则会显示某一个联系人信息,如果选择了联系人的某一项纪录,就会调用这个方法,可以通过点击选择联系人的某一项信息。如果希望可以对选择的某一项纪录进行进一步操作,比如直接拨打电话或调用邮箱发送邮件,则return YES。否则取消整个通讯录页面的显示并return NO。
===============================examples=======================================
例子:
- - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
- // assigning control back to the main controller
- [picker dismissModalViewControllerAnimated:YES];
- }
- - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker
- shouldContinueAfterSelectingPerson:(ABRecordRef)person {
- //获取联系人姓名
- name.text = (NSString*)ABRecordCopyCompositeName(person);
- //获取联系人电话
- ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);
- NSMutableArray *phones = [[NSMutableArray alloc] init];
- int i;
- for (i = 0; i < ABMultiValueGetCount(phoneMulti); i++) {
- NSString *aPhone = [(NSString*)ABMultiValueCopyValueAtIndex(phoneMulti, i) autorelease];
- NSString *aLabel = [(NSString*)ABMultiValueCopyLabelAtIndex(phoneMulti, i) autorelease];
- NSLog(@"PhoneLabel:%@ Phone#:%@",aLabel,aPhone);
- if([aLabel isEqualToString:@"_$!<Mobile>!$_"])
- {
- [phones addObject:aPhone];
- }
- }
- phoneNo.text=@"";
- if([phones count]>0)
- {
- NSString *mobileNo = [phones objectAtIndex:0];
- phoneNo.text = mobileNo;
- //NSLog(mobileNo);
- }
- //获取联系人邮箱
- ABMutableMultiValueRef emailMulti = ABRecordCopyValue(person, kABPersonEmailProperty);
- NSMutableArray *emails = [[NSMutableArray alloc] init];
- for (i = 0;i < ABMultiValueGetCount(emailMulti); i++)
- {
- NSString *emailAdress = [(NSString*)ABMultiValueCopyValueAtIndex(emailMulti, i) autorelease];
- [emails addObject:emailAdress];
- }
- email.text=@"";
- if([emails count]>0)
- {
- NSString *emailFirst=[emails objectAtIndex:0];
- email.text = emailFirst;
- //NSLog(emailFirst);
- }
- [peoplePicker dismissModalViewControllerAnimated:YES];
- return NO;
- }
- - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker
- shouldContinueAfterSelectingPerson:(ABRecordRef)person
- property:(ABPropertyID)property
- identifier:(ABMultiValueIdentifier)identifier{
- return NO;
- }