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

来源:互联网 发布:米6 手机无法连接网络 编辑:程序博客网 时间:2024/05/22 14:52
为了调用系统的通讯录界面与相应功能,需要引入AddressBook.framework与AddressBookUI.framework,
同时,在源文件中需要包含同文件,.
#import<AddressBook/AddressBook.h>#import<AddressBookUI/AddressBookUI.h>
首先申明变量:
ABPeoplePickerNavigationController *picker;
在需要的地方调用显示选择联系人界面,同时设置ABPeoplePickerNavigationControllerDelegate委托:
   picker = [[ABPeoplePickerNavigationController alloc] init];   picker.peoplePickerDelegate = self;   [self presentModalViewController:picker animated:YES];// showing the picker
下面的方法在用户选择通讯录一级列表的某一项时被调用,通过person可以获得选中联系人的所有信息,但当选中的联系人有多个号码,而我们又希望用户可以明确的指定一个号码时(如拨打电话),返回YES允许通讯录进入联系人详情界面
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker        shouldContinueAfterSelectingPerson:(ABRecordRef)person {             NSLog(@"用户选择了通讯录一级列表的某一项");             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 = (NSString*)ABMultiValueCopyValueAtIndex(phoneMulti, index);        //do something                [phone release];        [peoplePicker dismissModalViewControllerAnimated:YES];    }    return NO;}
最后还需要实现如下方法使得用户在点击"取消"按钮时关闭联系人选择界面:
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{    [peoplePicker dismissModalViewControllerAnimated:YES];    NSLog(@"回到主页面");}

原创粉丝点击