一、通讯录简介
用户还没有决定是否授权你的程序进行访问
iOS设备上的家长控制或其它一些许可配置阻止了你的程序与通讯录数据库进行交互
用户明确的拒绝了你的程序对通讯录的访问
用户已经授权给你的程序对通讯录进行访问
// 获得授权状态
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
三、申请授权
四、获得所有的联系人数据
// 获得所有的联系人
CFArrayRef array = ABAddressBookCopyArrayOfAllPeople(book);
// 联系人的总数
int count = CFArrayGetCount(array);
for (int i = 0; i < count; i++){
// 获得某个联系人
ABRecordRef person = CFArrayGetValueAtIndex(array, i);
}
// 释放
CFRelease(array);
NSArray *array = (__bridge NSArray *) ABAddressBookCopyArrayOfAllPeople(book);
int count = array.count;
for (int i = 0; i < count; i++){
ABRecordRef person = (__bridge ABRecordRef)array[i];
}
五、获得联系人的简单属性
// 获得名
CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
// 获得姓
CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
// 获得名
NSString *firstName = (__bridge NSString *) ABRecordCopyValue(person, kABPersonFirstNameProperty);
// 获得姓
NSString *lastName = (__bridge NSString *) ABRecordCopyValue(person, kABPersonLastNameProperty);
六、获得联系人的复杂属性
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
// …….
CFRelease(emails);
int count = ABMultiValueGetCount(emails);
for (int i = 0; i < count; i++){
// 获得标签名
CFStringRef emailLabel = ABMultiValueCopyLabelAtIndex(emails, i);
// 转为本地标签名(能看得懂的标签名,比如work、home)
CFStringRef localizedEmailLabel = ABAddressBookCopyLocalizedLabel(emailLabel);
// 获得邮件地址值
CFStringRef email = ABMultiValueCopyValueAtIndex(emails, i);
NSLog(@"%@-%@:%@", emailLabel, localizedEmailLabel, email);
// 释放
CFRelease(emailLabel);
CFRelease(localizedEmailLabel);
CFRelease(email);
}
// 获得标签名
NSString *emailLabel = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(emails, i));
// 转为本地标签名(能看得懂的标签名,比如work、home)
NSString *localizedEmailLabel = (__bridge NSString *)(ABAddressBookCopyLocalizedLabel((__bridge CFStringRef)(emailLabel)));
// 获得邮件地址值
NSString *email = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(emails, i));
NSLog(@"%@-%@:%@", emailLabel, localizedEmailLabel, email);
七、添加联系人的步骤
1.添加联系人代码实现
// 创建联系人
ABRecordRef person = ABPersonCreate();
// 设置属性
ABRecordSetValue(person, kABPersonFirstNameProperty, @"明杰", NULL);
ABRecordSetValue(person, kABPersonLastNameProperty, @"李", NULL);
// 通讯录实例
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 添加联系人
ABAddressBookAddRecord(book, person, NULL);
// 保存修改
ABAddressBookSave(book, NULL);
// 释放
CFRelease(person);
CFRelease(book);
八、添加一些复杂的属性
// 设置邮箱属性的内容
ABMultiValueRef email = ABMultiValueCreateMutable(kABStringPropertyType);
// 工作邮箱
ABMultiValueAddValueAndLabel(email, @"wok@qq.com", kABWorkLabel, NULL);
// 家庭邮箱
ABMultiValueAddValueAndLabel(email, @"home@qq.com", kABHomeLabel, NULL);
// 添加邮箱属性
ABRecordSetValue(person, kABPersonEmailProperty, email, NULL);
CFRelease(email);
九、添加群组的步骤
// 创建组
ABRecordRef group = ABGroupCreate();
// 设置组名
ABRecordSetValue(group, kABGroupNameProperty, @"家人", NULL);
// 通讯录实例
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 添加组
ABAddressBookAddRecord(book, group, NULL);
// 保存修改
ABAddressBookSave(book, NULL);
CFRelease(book);
CFRelease(group);
判断通讯录中的联系人是否有图片
取得图片数据(假如有的话)
设置联系人的图片数据
设置图片数据
if(ABPersonHasImageData(person)) {
NSData *data = (__bridge NSData *)ABPersonCopyImageData(person);
_imageView.image = [UIImage imageWithData:data];
}
1 // 2 // ViewController.m 3 // IOS_0323_访问通讯录 4 // 5 // Created by ma c on 16/3/23. 6 // Copyright © 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import11 12 @interface ViewController () 13 14 - (IBAction)accessAllPeople; 15 - (IBAction)updatePeople; 16 - (IBAction)addPeople; 17 18 @end 19 20 @implementation ViewController 21 22 - (void)viewDidLoad { 23 [super viewDidLoad]; 24 25 [self requestAccessAddressBook]; 26 27 } 28 29 //请求访问通讯录 30 - (void)requestAccessAddressBook 31 { 32 //创建通讯录实例 33 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); 34 35 //请求访问通讯录权限 36 ABAddressBookRequestAccessWithCompletion(book, ^(bool granted, CFErrorRef error) { 37 if (granted) { 38 NSLog(@"允许访问"); 39 // [self accessAddressBook]; 40 }else{ 41 NSLog(@"不允许访问"); 42 } 43 44 }); 45 46 //释放资源 47 CFRelease(book); 48 } 49 50 51 //访问通讯录信息 52 - (IBAction)accessAllPeople { 53 54 [self accessAllPeopleWithC]; 55 56 57 } 58 - (void)accessAllPeopleWithOC 59 { 60 //如果没有授权成功,直接返回 61 if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) return; 62 63 //1.创建通讯录实例 64 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); 65 66 //2.获得通讯录中所有联系人 67 //桥接技术: 68 NSArray *allPeople = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(book); 69 70 //3.遍历数组中所有联系人 71 for (int i = 0; i < allPeople.count; i++) { 72 ABRecordRef record = (__bridge ABRecordRef)(allPeople[i]); 73 74 NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(record, kABPersonFirstNameProperty)); 75 NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(record, kABPersonLastNameProperty)); 76 77 } 78 79 //7.释放 80 CFRelease(book); 81 } 82 83 84 - (void)accessAllPeopleWithC 85 { 86 //如果没有授权成功,直接返回 87 if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) return; 88 89 //1.创建通讯录实例 90 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); 91 92 //2.获得通讯录中所有联系人 93 CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(book); 94 95 //3.遍历数组中所有联系人 96 CFIndex count = CFArrayGetCount(allPeople); 97 98 for (CFIndex i = 0; i < count; i++) { 99 //4.获得i这个位置对应的联系人(1个联系人 对应 1个)100 ABRecordRef people = CFArrayGetValueAtIndex(allPeople, i);101 102 //5.取得联系人信息103 CFStringRef firstName = ABRecordCopyValue(people, kABPersonFirstNameProperty);104 CFStringRef lastName = ABRecordCopyValue(people, kABPersonLastNameProperty);105 ABMultiValueRef phone = ABRecordCopyValue(people, kABPersonPhoneProperty);106 107 //6.输出108 NSLog(@"%@ %@",lastName,firstName);109 110 //获得更详细的数据信息111 CFArrayRef phones = ABMultiValueCopyArrayOfAllValues(phone);112 CFIndex phoneCount = CFArrayGetCount(phones);113 for (int j = 0; j < phoneCount; j++) {114 CFStringRef phoneLabel = ABMultiValueCopyLabelAtIndex(phone, j);115 CFStringRef phoneValue = ABMultiValueCopyValueAtIndex(phone, j);116 NSLog(@"%@,%@", phoneLabel, phoneValue);117 118 119 CFRelease(phoneLabel);120 CFRelease(phoneValue);121 }122 123 //释放124 CFRelease(phones);125 CFRelease(phone);126 CFRelease(lastName);127 CFRelease(firstName);128 }129 130 //7.释放131 CFRelease(allPeople);132 CFRelease(book);133 134 }135 136 - (IBAction)updatePeople {137 //如果没有授权成功,直接返回138 if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) return;139 140 //1.创建通讯录实例141 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);142 143 //2.获得通讯录中所有联系人144 NSArray *allPeople = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(book);145 146 ABRecordRef people = (__bridge ABRecordRef)(allPeople[0]);147 CFStringRef lastName = (__bridge CFStringRef)@"刘";148 //3.修改姓149 ABRecordSetValue(people, kABPersonLastNameProperty, lastName, NULL);150 151 //4.同步152 ABAddressBookSave(book, NULL);153 154 //释放155 CFRelease(book);156 }157 158 - (IBAction)addPeople {159 // 如果没有授权成功,直接返回160 if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) return;161 162 // 1.创建新的联系人163 ABRecordRef people = ABPersonCreate();164 165 // 2.设置信息166 ABRecordSetValue(people, kABPersonLastNameProperty, (__bridge CFStringRef)@"刘", NULL);167 ABRecordSetValue(people, kABPersonFirstNameProperty, (__bridge CFStringRef)@"蛋疼", NULL);168 169 ABMultiValueRef phone = ABMultiValueCreateMutable(kABMultiStringPropertyType);170 ABMultiValueAddValueAndLabel(phone, (__bridge CFStringRef)@"10010", kABPersonPhoneMainLabel, NULL);171 ABMultiValueAddValueAndLabel(phone, (__bridge CFStringRef)@"10011", kABPersonPhoneMobileLabel, NULL);172 ABMultiValueAddValueAndLabel(phone, (__bridge CFStringRef)@"10012", kABPersonPhoneIPhoneLabel, NULL);173 ABRecordSetValue(people, kABPersonPhoneProperty, phone, NULL);174 175 // 3.添加联系人到通讯录176 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);177 ABAddressBookAddRecord(book, people, NULL);178 179 ABAddressBookSave(book, NULL);180 181 // 4.释放182 CFRelease(phone);183 CFRelease(people);184 CFRelease(book);185 186 }187 @end
1 // 2 // ViewController.m 3 // 02-通讯录访问 4 // 5 // Created by apple on 13-9-14. 6 // Copyright (c) 2013年 itcast. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import11 12 @interface ViewController () 13 14 @end 15 16 @implementation ViewController 17 18 - (void)viewDidLoad 19 { 20 [super viewDidLoad]; 21 22 // 获得状态 23 ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus(); 24 25 // 获得所有的联系人数据 26 if (status == kABAuthorizationStatusAuthorized) { 27 [self getPersonImage]; 28 } 29 } 30 31 - (void)getPersonImage 32 { 33 // 1.获得通讯录实例 34 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); 35 36 // 2.获得通讯录实例中的所有联系人数组 37 CFArrayRef persons = ABAddressBookCopyArrayOfAllPeople(book); 38 39 ABRecordRef person = CFArrayGetValueAtIndex(persons, 0); 40 if (ABPersonHasImageData(person)) { 41 NSData *data = (__bridge NSData *)(ABPersonCopyImageData(person)); 42 43 44 _imageView.image = [UIImage imageWithData:data]; 45 46 // [data writeToFile:@"/Users/apple/Desktop/abc.png" atomically:YES]; 47 } 48 49 // 释放资源 50 CFRelease(book); 51 CFRelease(persons); 52 } 53 54 - (void)setPersonImage 55 { 56 // 1.获得通讯录实例 57 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); 58 59 // 2.获得通讯录实例中的所有联系人数组 60 CFArrayRef persons = ABAddressBookCopyArrayOfAllPeople(book); 61 62 // 3.遍历所有的联系人数据 63 int count = CFArrayGetCount(persons); 64 65 // 加载头像数据 66 UIImage *image = [UIImage imageNamed:@"test2.png"]; 67 NSData *data = UIImagePNGRepresentation(image); 68 69 for (int i = 0; i