一.基本知识
和通讯录中联系人相关的应用iPhone提供了两个框架:AddressBook.framework和AddressBookUI.framework,使用这两个框架我们可以在程序中访问并显示iPhone数据库中的联系人信息。本地相册使用了UIImagePickerViewController
二.具体介绍
1.AddressBookUI显示部分
AddressBookUI中提供了和联系人显示信息相关的一些Controller,有四个:
ABPeoplePickerNavigationController:显示整个通讯录并可以选择一个联系人的信息
ABPersonViewController:显示一个具体联系人的信息
ABNewPersonViewController:增加一个新的联系人
ABUnknownPersonViewController:完善一个联系人的信息
由于其中最主要的是ABPeoplePickerNavigationController,因此就具体的介绍一下通过程序显示整个通讯录并且可以选择其中某个联系人信息的步骤。
ABPeoplePickerNavigationController *picker;
在需要的地方调用显示选择联系人界面,同时设置ABPeoplePickerNavigationControllerDelegate委托:
if(!picker){
picker = [[ABPeoplePickerNavigationController alloc] init];
// place the delegate of the picker to the controll
picker.peoplePickerDelegate = self;
}
// showing the picker
[self presentModalViewController:picker animated:YES];
选择联系人界面如下图所示:

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
return YES;
}
该方法在用户选择通讯录一级列表的某一项时被调用,通过person可以获得选中联系人的所有信息,但当选中的联系人有多个号码,而我们又希望用户可以明确的指定一个号码时(如拨打电话),返回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 = (NSString*)ABMultiValueCopyValueAtIndex(phoneMulti, index);
//do something
[phone release]
[peoplePicker dismissModalViewControllerAnimated:YES];
------调用本地相册实例
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = sourceType;
[self presentModalViewController:picker animated:YES];
[picker release];
#pragma mark Camera View Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{ [picker dismissModalViewControllerAnimated:YES];
UIImage *image = [[info objectForKey:UIImagePickerControllerEditedImage] retain];
[self performSelector:@selector(saveImage:) withObject:image afterDelay:0.5];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{ [picker dismissModalViewControllerAnimated:YES];
}
本文介绍了如何使用AddressBookUI框架中的ABPeoplePickerNavigationController来显示iPhone通讯录,并允许用户选择联系人及其详细信息,包括电话号码等。
7533

被折叠的 条评论
为什么被折叠?



