`

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

 
阅读更多

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

首先申明变量:

ABPeoplePickerNavigationController*picker;

在需要的地方调用显示选择联系人界面,同时设置ABPeoplePickerNavigationControllerDelegate委托:

if(!picker){

picker= [[ABPeoplePickerNavigationControlleralloc]init];

// place the delegate of the picker to the controll

picker.peoplePickerDelegate=self;

}

// showing the picker

[selfpresentModalViewController:pickeranimated:YES];


选择联系人界面如下图所示:

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController*)peoplePicker

shouldContinueAfterSelectingPerson:(ABRecordRef)person

{

returnYES;

}

该方法在用户选择通讯录一级列表的某一项时被调用,通过person可以获得选中联系人的所有信息,但当选中的联系人有多个号码,而我们又希望用户可以明确的指定一个号码时(如拨打电话),返回YES允许通讯录进入联系人详情界面:


当用户点击某个字段时,会调用如下方法:

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController*)peoplePicker

shouldContinueAfterSelectingPerson:(ABRecordRef)person

property:(ABPropertyID)property

identifier:(ABMultiValueIdentifier)identifier

{

if(property ==kABPersonPhoneProperty) {

ABMutableMultiValueRefphoneMulti =ABRecordCopyValue(person, property);

intindex =ABMultiValueGetIndexForIdentifier(phoneMulti,identifier);

NSString*phone = (NSString*)ABMultiValueCopyValueAtIndex(phoneMulti, index);

//do something

[phone release];

[peoplePickerdismissModalViewControllerAnimated:YES];

}


returnNO;


}

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


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

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController*)peoplePicker

{

// assigning control back to the main controller

[pickerdismissModalViewControllerAnimated:YES];

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics