带UI的通讯录
导入AddressBookUI.framework框架
首先在AppDelegate里面请求授权
/*
kABAuthorizationStatusNotDetermined 未授权
kABAuthorizationStatusRestricte 受限制
kABAuthorizationStatusDenied 被拒绝
kABAuthorizationStatusAuthorized 已授权
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 1.获取授权状态
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
// 2.判断授权状态
if (status == kABAuthorizationStatusNotDetermined) {//未授权
// 3.创建通讯录
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, nil);
//4.请求授权
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
NSLog(@"授权成功");
}
});
}
return YES;
}
ViewController
首先导入头文件,遵守协议
#import <AddressBookUI/AddressBookUI.h>
@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate>
@end
创建联系人选择控制器,设置代理 弹出控制器
// 创建联系人选择控制器
ABPeoplePickerNavigationController *peopleNC = [[ABPeoplePickerNavigationController alloc] init];
// 千万不要写成delegate
peopleNC.peoplePickerDelegate = self;
// 弹出控制器
[self presentViewController:peopleNC animated:YES completion:nil];
// 点击取消按钮时调用
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
NSLog(@"peoplePickerNavigationControllerDidCancel");
}
下面两个方法同时存在默认调用第一个方法
// 点击某个联系人时调用
// peoplePicker 控制器
// person具体的联系人
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// 姓名和电话
// 1.获取姓名 取值:copy get
CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
// coreFoundation框架转换为foundation -->桥接
// 桥接方式一:手动管理内存
// NSString *firstNameStr = (__bridge NSString *)(firstName);
// NSString *lastNameStr = (__bridge NSString *)(lastName);
// NSLog(@"firstName: %@ lastName: %@",firstNameStr,lastNameStr);
// CFRelease(firstName);
// CFRelease(lastName);
// 桥接方式二: 2.1不需要手动管理内存
// NSString *firstNameStr = CFBridgingRelease(firstName);
// NSString *lastNameStr = CFBridgingRelease(lastName);
// NSLog(@"firstName: %@ lastName: %@",firstNameStr,lastNameStr);
// 2.2将对象的所有权转换
NSString *firstNameStr = (__bridge_transfer NSString *)firstName;
NSString *lastNameStr = (__bridge_transfer NSString *)lastName;
NSLog(@"firstName: %@ lastName: %@",firstNameStr,lastNameStr);
// 桥接方式三: Foundation 转换为CoreFoundation 不常用
// (__bridge_retained <#CF type#>)<#expression#>)
// 2.获取电话
// ABMultiValueRef类似数组 里面放的是类似字典
// 获取所有的电话
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
// 获取长度
CFIndex phonesCount = ABMultiValueGetCount(phones);
// 获取每一个电话
for (int i=0; i<phonesCount; i++) {
// 标签
NSString *label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, i));
// 电话
NSString *value = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, i));
NSLog(@"label:%@ value:%@",label,value);
}
CFRelease(phones);
}
}
// 点击某个联系人的详细属性
// property属性的ID
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
NSLog(@"%d",property);
NSLog(@"%s",__func__);
}
不带UI的通讯录界面
导入AddressBook.framework框架
请求授权的操作是一样的
获取通讯录,没有布局页面
// 1.获取所有的数据
// 1.1获取通讯录
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, nil);
// 1.2获取所有联系人的数据
CFArrayRef peoples = ABAddressBookCopyArrayOfAllPeople(addressBook);
// 2.获取某一个人的数据
// 2.1获取长度
CFIndex peoplesCount = CFArrayGetCount(peoples);
// 2.2遍历数组
for (int i=0; i<peoplesCount; i++) {
// 2.3获取具体联系人的数据
ABRecordRef person = CFArrayGetValueAtIndex(peoples, i);
// 3.获取电话和姓名
// 3.1获取姓名
NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSLog(@"firstName:%@ lastName:%@",firstName,lastName);
// 3.2获取电话
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
// 3.3获取长度
CFIndex phonesCount = ABMultiValueGetCount(phones);
// 3.4遍历所有电话
for (int i=0; i<phonesCount; i++) {
NSString *label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, i));
NSString *value = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, i));
NSLog(@"label %@ value %@",label,value);
}
CFRelease(phones);
}
CFRelease(addressBook);
CFRelease(peoples);
iOS9之后的通讯录
导入ContactsUI.framework框架
请求授权
/*
CNAuthorizationStatusNotDetermined 未授权
CNAuthorizationStatusRestricted 受限制
CNAuthorizationStatusDenied 被拒绝
CNAuthorizationStatusAuthorized 已授权
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 1.获取授权状态
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
// 2.判断授权状态
if (status == CNAuthorizationStatusNotDetermined) {
// 3.创建通讯录
CNContactStore *contactStore = [[CNContactStore alloc] init];
// 4.请求授权
[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"授权成功");
}
}];
}
return YES;
}
ViewController
设置协议和头文件
#import <ContactsUI/ContactsUI.h>
#import <AddressBookUI/AddressBookUI.h>
@interface ViewController ()<CNContactPickerDelegate>
@end
跳转到通讯录操作
// 1.创建控制器
CNContactPickerViewController *contactNC = [[CNContactPickerViewController alloc] init];
// 2.设置代理
contactNC.delegate = self;
// 3.弹出控制器
[self presentViewController:contactNC animated:YES completion:nil];
以下两个方法,如果实现了第一个,第二个不会调用
// 选中联系人时调用
// contact 选择的联系人数据
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact
{
// 姓名和电话
// 1. 姓名
NSLog(@"givenName: %@,familyName :%@",contact.givenName,contact.familyName);
// 2.电话
NSArray *phoneNums = contact.phoneNumbers;
for (CNLabeledValue *labeledValue in phoneNums) {
NSLog(@"label:%@",labeledValue.label);
CNPhoneNumber *phoneNum = labeledValue.value;
NSLog(@"phoneNumber :%@",phoneNum.stringValue);
}
}
和
// 选取联系人某一个属性时调用
-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty
{
}
网友评论