iOS调用系统通讯录获取姓名电话号码(转)
原文地址:http://blog.csdn.net/idoshi201109/article/details/46007125
OS调用系统通讯录获取姓名电话号码
(iOS 8.0 Xcode6.3可以使用)
1、导入头文件。
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
2、添加协议
@interface NewAddressViewController ()<ABPeoplePickerNavigationControllerDelegate>
3、实例化
ABPeoplePickerNavigationController * vc = [[ABPeoplePickerNavigationController alloc] init];
vc.peoplePickerDelegate = self;
[self presentViewController:vc animated:YES completion:nil];
4、实现协议方法
#pragma mark – ABPeoplePicker delegate
-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
//获取联系人的电话 引用属性
ABMutableMultiValueRef valuesRef=ABRecordCopyValue(person, kABPersonPhoneProperty);
//获取选中的 标识符 在联系人中的 索引
CFIndex index=ABMultiValueGetIndexForIdentifier(valuesRef, identifier);
//读取电话号码
CFStringRef telValue=ABMultiValueCopyValueAtIndex(valuesRef, index);
//获取姓名
CFStringRef FullName=ABRecordCopyCompositeName(person);
//添加到数组中
NSDictionary *dict=@{@”name”:(__bridge NSString *)FullName,@”phone”:(__bridge NSString *)telValue};
[arrayData addObject:dict];
[Mytableview reloadData];
}
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
[self dismissViewControllerAnimated:YES completion:^{
[Mytableview reloadData];
}];
}
// 8.0之前才会调用
– (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
return YES;
}
#pragma ios8之前调用
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
//获取联系人的电话 引用属性
ABMutableMultiValueRef valuesRef=ABRecordCopyValue(person, kABPersonPhoneProperty);
//获取选中的 标识符 在联系人中的 索引
CFIndex index=ABMultiValueGetIndexForIdentifier(valuesRef, identifier);
//读取电话号码
CFStringRef telValue=ABMultiValueCopyValueAtIndex(valuesRef, index);
//获取姓名
CFStringRef FullName=ABRecordCopyCompositeName(person);
//添加到数组中
NSDictionary *dict=@{@”name”:(__bridge NSString *)FullName,@”phone”:(__bridge NSString *)telValue};
[arrayData addObject:dict];
[Mytableview reloadData];
[self dismissViewControllerAnimated:YES completion:nil];
return false; //返回yes 会拨打电话,no 不会拨打电话
}