iOS 通讯录(OC 语言)
来源:互联网 发布:杰奇网络注册 编辑:程序博客网 时间:2024/06/05 03:10
通讯录
有两个框架可以访问用户的通讯录
AddressBookUI.framework
- 提供了联系人列表界面,联系人详情界面,添加联系人界面等
- 一般用于选择联系人
AddressBook.framework
- 纯 C 语言的 API ,仅仅是获得联系人数据
- 没有提供 UI 界面展示,需要自己搭建联系人展示界面
- 里面的数据类型大部分是基于 Core Foundation 框架,使用很头疼
从 iOS6开始,需要得到用户的授权才能访问通讯录,因此在使用之前,需要检查用户是否已经授权
- 请求授权
//1.获取授权状态ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();//2.判断是否授权if (status == kABAuthorizationStatusNotDetermined) { //3.创建通讯录 ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL); //4.请求授权 ABAddressBookRequestAccessWithCompletion( addressBook, ^(bool granted, CFErrorRef error) { if (error) { NSLog(@"授权失败"); }else{ NSLog(@"授权成功"); } }); }
通讯录(带 UI)事例
#import "ViewController.h"#import <AddressBookUI/AddressBookUI.h>@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate>@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor];}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //1.创建控制器 ABPeoplePickerNavigationController *peopleNavC = [[ABPeoplePickerNavigationController alloc] init]; peopleNavC.peoplePickerDelegate = self; //2.弹出控制器 [self presentViewController:peopleNavC animated:YES completion:nil];}//// Called after a person has been selected by the user.- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person{ CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty); CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);// //桥接方式1 -- 需要手动管理内存// NSString *first = (__bridge NSString*)(firstName);// NSString *lastt = (__bridge NSString*)(lastName);// // //释放方式1// CFRelease(firstName);// CFRelease(lastName); //桥接方式2 -- 不需要手动管理内存 ---->转变为 ARC// NSString *first = CFBridgingRelease( firstName);// NSString *lastt = CFBridgingRelease( lastName); //桥接方式2 -- 不需要手动管理内存 ---->转换对象管理权-- 交给 Foundation 管理 NSString *first = (__bridge_transfer NSString*)( firstName); NSString *lastt = (__bridge_transfer NSString*)( lastName); //桥接方式3 -- Foundation 框架-->Core Foundation //CFStringRef firstRef = (__bridge_retained CFStringRef)first; //获取电话 ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty); long count = ABMultiValueGetCount(phones); for (int i = 0 ; i < count; i++) { NSString * label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, i)); NSString *value = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, i)); NSLog(@"%@,%@",label,value); } CFRelease(phones); NSLog(@"%@:%@",first,lastt);}
不带 UI 的通讯录(使用AddressBook.framework)
#import <AddressBook/AddressBook.h>#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) { //1.获取所有通讯录的数据 ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL,NULL); CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook); CFIndex count = CFArrayGetCount(people); //2.选择某一个数据 for (int i = 0; i < count; i++) { ABRecordRef record = CFArrayGetValueAtIndex(people, i); NSString *firstName = CFBridgingRelease(ABRecordCopyValue(record, kABPersonFirstNameProperty)); NSLog(@"%@",firstName); //获取所有电话数据 ABMultiValueRef phones = ABRecordCopyValue(record, kABPersonPhoneProperty); CFIndex phonesCount = ABMultiValueGetCount(phones); //遍历电话数据 for (int j = 0; j < phonesCount; j++) { NSString *label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, j)); NSString *value = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, j)); NSLog(@"%@,%@",label,value); } //释放 phones CFRelease(phones); } //释放 people CFRelease(people); //释放addressBook CFRelease(addressBook); }}@end
iOS9通讯录使用
- 获取授权
//1.获取授权状态 CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]; //2.判断授权状态 if (status == CNAuthorizationStatusNotDetermined) { //3.创建通讯录 CNContactStore *contact = [[CNContactStore alloc] init]; //4.发送请求 [contact requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { if (error) { NSLog(@"授权失败"); }else{ NSLog(@"授权成功!"); } }]; }
- 获取通讯录信息
#import "ViewController.h"#import <ContactsUI/ContactsUI.h>@interface ViewController ()<CNContactPickerDelegate>@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad];}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //创建控制器 CNContactPickerViewController *cpVC = [[CNContactPickerViewController alloc] init]; //弹出控制器 [self presentViewController:cpVC animated:YES completion:nil]; cpVC.delegate = self;}-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{ //1.判断授权状态 if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusAuthorized) { //获取名字 NSLog(@"%@.%@",contact.givenName,contact.familyName); //获取电话号码 NSArray *phoneNumbers = contact.phoneNumbers; for (CNLabeledValue *labeledValue in phoneNumbers) { NSLog(@"%@--%@",labeledValue.label, labeledValue.value); } }}@end
0 0
- iOS 通讯录(OC 语言)
- 蓝鸥iOS从零基础到精通就业-OC语言入门 通讯录1
- 蓝鸥iOS从零基础到精通就业-OC语言入门 通讯录2
- 蓝鸥iOS从零基础到精通就业-OC语言入门 通讯录3
- OC语言__实现中等难度通讯录
- OC语言实现中等难度通讯录
- iOS前期OC训练OC_通讯录
- OC通讯录
- 通讯录 oc
- OC通讯录
- OC语言,逻辑性的模拟通讯录,大家可以作参考
- iOS开发--OC篇--OC语言简介
- 【黑马训练营】IOS C语言通讯录
- iOS OC语言(二) 类
- iOS OC语言(三) 继承
- IOS之OC语言简介
- IOS之OC语言基础知识
- OC: 用OC语言完成简易通讯录(实现增删改查)功能.
- Codeforces 628D Magic Numbers 【数位dp】
- java(16)--利用session存储购买商品
- NIO教程 详细版
- 实时采集数据的动态曲线控件
- cocos-html5 button: setEnabled setBright
- iOS 通讯录(OC 语言)
- nginx伪静态配置实例
- 实现Swipe View
- ubuntu初探4---ubuntu安装jdk8&eclipse
- OC 闭源库
- Android项目使用support v7时遇到的各种问题
- onenote发送至博客实践
- AsciiDoc 相关资源
- Android&java的成长之路之四(自定义字母索引)