您的位置:首页 > 其它

iPhone程序调用系统通讯录选择单个电话号码

2010-12-20 20:17 302 查看
为了调用系统的通讯录界面与相应功能,需要引入AddressBook.framework与AddressBookUI.framework,同时,在源文件中需要包含同文件<AddressBook/AddressBook.h>,<AddressBookUI/AddressBookUI.h>.

首先申明变量:

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];

}

return NO;

}
联系人信息中可能有很多字段,首先需要判断选择的是否为电话号码字段.当满足要求时,获取联系人信息,通过标识符获得用户选择的号码在该联系人号码列表中的索引,最后通过索引获得选中的电话号码.

最后还需要实现如下方法使得用户在点击"取消"按钮时关闭联系人选择界面:

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
// assigning control back to the main controller
[picker dismissModalViewControllerAnimated:YES];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐