标签:
.h文件
//系统自带的联系人framework
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
加委托
ABPeoplePickerNavigationControllerDelegate
{
UITextField * aPhoneField;//患者手机号Field
}
/*
picker
系统内置的联系人view对象
@property (nonatomic)ABPeoplePickerNavigationController *picker
Discussion
此对象来自于引入的AddressBookUI.framework,系统内置的联系人view对象
*/
@property (nonatomic)ABPeoplePickerNavigationController *picker;
.m文件
//点击按钮弹出联系人列表
-(void)btnClick:(UIButton *)sender{
if(!self.picker){
self.picker = [[ABPeoplePickerNavigationController alloc] init];
// place the delegate of the picker to the controll
// self.picker.modalPresentationStyle = UIModalPresentationCurrentContext;
// self.picker.modalInPopover = YES;
// self.picker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
self.picker.peoplePickerDelegate = self;
}
//显示一个viewcontroller
[self presentViewController:self.picker animated:YES completion:nil];
}
/*
Discussion
该方法在用户选择通讯录一级列表的某一项时被调用,通过person可以获得选中联系人的所有信息,但当选中的联系人有多个号码,而我们又希望用户可以明确的指定一个号码时(如拨打电话),返回YES允许通讯录进入联系人详情界面:
*/
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
NSString* phone;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,
kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
} else {
phone = @"";
}
phone=[phone stringByReplacingOccurrencesOfString:@"-" withString:@""];
aPhoneField.text=phone;
CFRelease(phoneNumbers);
[self.picker dismissViewControllerAnimated:YES completion:nil];
return NO;
}
/*
Discussion
当用户进入单个联系人信息(二级页面)点击某个字段时,会调用如下方法,返回YES继续进入下一步,点击NO不进入下一步,比如点击电话,返回YES就拨打电话,返回NO不会拨打电话:
*/
- (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 stringWithFormat:@"%@",ABMultiValueCopyValueAtIndex(phoneMulti, index)];
aPhoneField.text=phone;
[self.picker dismissViewControllerAnimated:YES completion:nil];
}
return NO;
}
/*
Discussion
当用户离开单个联系人信息(二级页面)点击某个字段时调用
*/
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
[self.picker dismissViewControllerAnimated:YES completion:nil];
}
//ios8之后发生了改变,因为iOS8以后通讯录的结构有所变化:第一层是人名列表,点击某个人名进去之后是这个人的详细信息。
//第一个方法是选中这个人之后调用。
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person NS_AVAILABLE_IOS(8_0){
NSString* phone;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,
kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
} else {
phone = @"";
}
phone=[phone stringByReplacingOccurrencesOfString:@"-" withString:@""];
aPhoneField.text=phone;
CFRelease(phoneNumbers);
[self.picker dismissViewControllerAnimated:YES completion:nil];
}
//第二个方法是选中这个人的详细信息后调用。
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0){
if (property == kABPersonPhoneProperty) {
ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, property);
int index = ABMultiValueGetIndexForIdentifier(phoneMulti,identifier);
NSString* phone = [NSString stringWithFormat:@"%@",ABMultiValueCopyValueAtIndex(phoneMulti, index)];
aPhoneField.text=phone;
[self.picker dismissViewControllerAnimated:YES completion:nil];
}
}
标签:
原文地址:http://www.cnblogs.com/huangzs/p/4480689.html