1. 带UI的通讯录
#import "ViewController.h"
#import <AddressBookUI/AddressBookUI.h>
@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate>
@end
@implementation ViewController
// 触摸显示
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//1. 创建联系人选择控制器
ABPeoplePickerNavigationController *picker = [ABPeoplePickerNavigationController new];
//2. 设置代理 --> 注意不是 Delegate
picker.peoplePickerDelegate = self;
//3. 模态视图弹出
[self presentViewController:picker animated:YES completion:nil];
}
#pragma mark 选中某个联系人时调用
/**
以下两个方法,如果同时实现, 只会运行didSelectPerson: 方法
两个方法, 应该实现一个即可
*/
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
//需求: 获取姓名 和 电话
//1. 获取姓名
//Record --> 就相当于一条记录
//lastName: 姓
CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *lastNameStr = (__bridge NSString *)(lastName);
NSLog(@"lastNameStr: %@",lastNameStr);
CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *firstNameStr = (__bridge NSString *)(firstName);
NSLog(@"firstNameStr: %@",firstNameStr);
CFRelease(lastName);
CFRelease(firstName);
//2. 获取电话 --> 电话是多数据类型
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
//2.1 获取电话的个数
CFIndex count = ABMultiValueGetCount(phones);
//2.2 遍历联系人
for (CFIndex i = 0 ; i < count; i++) {
//2.3 获取联系电话的标签
NSString *label = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
NSLog(@"label: %@",label);
//2.4 获取联系电话
NSString *value = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, i));
NSLog(@"value: %@",value);
}
//3. 释放 CF 对象
CFRelease(phones);
//Core Foundation 和 Fundation 桥接的问题 --> 面试题
//1. 有几种方式 2. 注意事项 注意释放 CF 对象
//将 CF 对象给 Foundatian
//1. (__bridge type)<#expression#>) : 只是让 Foundation 框架暂时使用 CF 框架对象
//2. (__bridge_transfer )) / CFBridgingRelease : CF框架移交对象的管理权给 Foundation 框架
//将 Foundatian给 CF 对象 不常用
//3. (__bridge_retained <#CF type#>)<#expression#>)
}
#pragma mark 选中联系人的某个属性的时候调用
//- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
//{
// NSLog(@"%d",property);
//}
#pragma mark 取消选中联系人时调用
// 在 iOS7下, 必须实现此方法, 否则就会崩溃
//- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
//{
// NSLog(@"%s",__func__);
//}
@end
2. 不带UI的通讯录
1). AppDelegate授权
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//通讯录授权 --> iOS6开始, 必须授权才能上架
/**
kABAuthorizationStatusNotDetermined = 0 用户未选择
kABAuthorizationStatusRestricted, 一些许可配置阻止了通讯录的交互
kABAuthorizationStatusDenied, 用户拒绝
kABAuthorizationStatusAuthorized 用户授权
*/
//1. 获取授权状态
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
//2. 创建 AddrssBook
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
//3. 没有授权时就授权
if (status == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
//3.1 判断出错
if (error) {
return;
}
//3.2 判断是否授权
if (granted) {
NSLog(@"已经授权");
} else {
NSLog(@"没有授权");
}
});
}
//4. 释放 CF 对象
CFRelease(addressBook);
return YES;
}
2). 代码
#import "ViewController.h"
#import <AddressBook/AddressBook.h>
@implementation ViewController
// 触摸显示
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 点击屏幕获取所有联系人信息
//1. 判断是否授权成功, 授权成功才能获取数据
if ( ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
//2. 创建通讯录
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
//3. 获取联系人 --> Array
CFArrayRef peosons = ABAddressBookCopyArrayOfAllPeople(addressBook);
//4. 遍历联系人来获取数据(姓名和电话)
CFIndex count = CFArrayGetCount(peosons);
for (CFIndex i = 0 ; i < count; i++) {
//5. 获取单个联系人
ABRecordRef person = CFArrayGetValueAtIndex(peosons, i);
//6. 获取姓名
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSLog(@"lastName: %@, firstName: %@", lastName, firstName);
//7. 获取电话
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
//7.1 获取 count
CFIndex phoneCount = ABMultiValueGetCount(phones);
//7.2 遍历ABMultiValueRef
for (CFIndex i = 0; i < phoneCount; i++) {
NSString *label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, i));
NSString *value = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, i));
NSLog(@"label: %@, value: %@",label, value);
}
NSLog(@"\n\n");
//8.1 释放 CF 对象
CFRelease(phones);
}
//8.1 释放 CF 对象
CFRelease(peosons);
CFRelease(addressBook);
}
}
@end
3. iOS9 通讯录(新增)
#import "ViewController.h"
#import <ContactsUI/ContactsUI.h>
@interface ViewController () <CNContactPickerDelegate>
@end
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 1. 创建控制器
CNContactPickerViewController *contact = [CNContactPickerViewController new];
// 2. 设置代理
contact.delegate = self;
// 3. 模态弹出
[self presentViewController:contact animated:YES completion:nil];
}
#pragma mark 选择联系人的时候调用
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
// 1. 获取姓名
NSLog(@"givenName: %@, familyName: %@", contact.givenName, contact.familyName);
// 2. 获取电话
for (CNLabeledValue *labeledValue in contact.phoneNumbers) {
CNPhoneNumber *phoneNumber = labeledValue.value;
NSLog(@"phoneNumber: %@", phoneNumber.stringValue);
}
}
#pragma mark 实现了此方法就可以选择多个联系人
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact *> *)contacts {
for (CNContact *contact in contacts) {
NSLog(@"givenName: %@, familyName: %@", contact.givenName, contact.familyName);
for (CNLabeledValue *labeledValue in contact.phoneNumbers) {
NSLog(@"label: %@", labeledValue.label);
CNPhoneNumber *phoneNumber = labeledValue.value;
NSLog(@"phoneNumber: %@", phoneNumber.stringValue);
}
}
}
@end