您的位置:首页 > 移动开发 > IOS开发

iOS调用系统通讯录获取姓名电话号码(转)

2015-08-16 15:00 555 查看
原文地址: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 不会拨打电话

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: