一、通讯录简介
iOS提供了AddressBook.framework框架,允许开发者与设备中的通讯录进行数据交互
二、查询授权状态
可以调用ABAddressBookGetAuthorizationStatus函数进行查询授权状态,返回值有以下几种情况:
kABAuthorizationStatusNotDetermined = 0
用户还没有决定是否授权你的程序进行访问
kABAuthorizationStatusRestricted = 1
iOS设备上的家长控制或其它一些许可配置阻止了你的程序与通讯录数据库进行交互
kABAuthorizationStatusDenied = 2
用户明确的拒绝了你的程序对通讯录的访问
kABAuthorizationStatusAuthorized = 3
用户已经授权给你的程序对通讯录进行访问
// 获得授权状态
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
三、申请授权
如果应用的授权状态是kABAuthorizationStatusNotDetermined,那么可以使用 ABAddressBookRequestAccessWithCompletion函数请求用户授权对通讯录的访问
ABAddressBookRequestAccessWithCompletion函数的第1个参数是通讯录实例、第2个参数是一个block,无论授权结果如何,都会调用,并传入一个bool变量表示授权成功还是失败
一般都是在程序启动完毕后就申请授权(在AppDelegate的application:didFinishLaunchingWithOptions:方法中)
四、获得所有的联系人数据
1.授权成功后,可以通过调用ABAddressBookCopyArrayOfAllPeople获得所有的联系人数据
这个函数返回一个CFArrayRef类型的数组
// 获得所有的联系人
CFArrayRef array = ABAddressBookCopyArrayOfAllPeople(book);
// 联系人的总数
int count = CFArrayGetCount(array);
for (int i = 0; i < count; i++){
// 获得某个联系人
ABRecordRef person = CFArrayGetValueAtIndex(array, i);
}
// 释放
CFRelease(array);
通讯录数组中的每条记录都是一个ABRecordRef类型的数据,可以是一个群或一个人
2.为了方便操作,也可以将CFArrayRef转成NSArray类型的数组
NSArray *array = (__bridge NSArray *) ABAddressBookCopyArrayOfAllPeople(book);
int count = array.count;
for (int i = 0; i < count; i++){
ABRecordRef person = (__bridge ABRecordRef)array[i];
}
五、获得联系人的简单属性
一个联系人就是一个ABRecordRef,每个联系人都有自己的属性,比如名字、电话、邮件等
使用ABRecordCopyValue函数可以从ABRecordRef中获得联系人的属性
ABRecordCopyValue函数接收2个参数,第1个参数是ABRecordRef实例,第2个参数决定想要获得哪个属性
// 获得名
CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
// 获得姓
CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
kABPersonFirstNameProperty和kABPersonLastNameProperty属性返回的都是CFStringRef类型的数据,由于是Copy出来的,最后需要CFRelease一下
所有的属性常量值都定义在了ABPerson.h头文件中
为了方便操作,也可以将CFStringRef转为 NSString进行操作
// 获得名
NSString *firstName = (__bridge NSString *) ABRecordCopyValue(person, kABPersonFirstNameProperty);
// 获得姓
NSString *lastName = (__bridge NSString *) ABRecordCopyValue(person, kABPersonLastNameProperty);
六、获得联系人的复杂属性
前面获得了联系人的姓、名属性值,都是非常简单的,一个属性对应一个字符串值
联系人的有些属性值就没这么简单,一个属性可能会包含多个值
比如邮箱,分为工作邮箱、住宅邮箱、其他邮箱等
比如电话,分为工作电话、住宅电话、其他电话等
如果是复杂属性,那么ABRecordCopyValue函数返回的就是ABMultiValueRef类型的数据
比如获取邮件数据
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);
}
如果不想管理内存,可以将CFStringRef转为NSString
// 获得标签名
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);
七、添加联系人的步骤
添加联系人的步骤
通过ABPersonCreate函数创建一个新的联系人(返回ABRecordRef)
通过ABRecordSetValue函数设置联系人的属性
通过ABAddressBookAddRecord函数将联系人添加到通讯录数据库中
通过ABAddressBookSave函数保存刚才所作的修改
可以通过ABAddressBookHasUnsavedChanges函数判断是否有未保存的修改
当决定是否更改通讯录数据库后,你可以分别使用 AbAddressBookSave 或 ABAddressBookRevert 方式来保存或放弃更改
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, @"[email protected]", kABWorkLabel, NULL);
// 家庭邮箱
ABMultiValueAddValueAndLabel(email, @"[email protected]", kABHomeLabel, NULL);
// 添加邮箱属性
ABRecordSetValue(person, kABPersonEmailProperty, email, NULL);
CFRelease(email);
九、添加群组的步骤
添加群组的步骤大体和添加联系人一致
通过ABPersonCreate函数创建一个新的组(返回ABRecordRef)
通过ABRecordSetValue函数设置组名
通过ABAddressBookAddRecord函数将组添加到通讯录数据库中
通过ABAddressBookSave函数保存刚才所作的修改
1.添加群组代码实现
// 创建组
ABRecordRef group = ABGroupCreate();
// 设置组名
ABRecordSetValue(group, kABGroupNameProperty, @"家人", NULL);
// 通讯录实例
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 添加组
ABAddressBookAddRecord(book, group, NULL);
// 保存修改
ABAddressBookSave(book, NULL);
CFRelease(book);
CFRelease(group);
上面代码添加的是一个空的群组,里面没有任何联系人
十、其他函数
如果想获取所有的群组信息,可以使用ABAddressBookCopyArrayOfAllGroups函数
如果想添加联系人到组中,可以使用ABGroupAddMember函数
如果想从组中移除联系人,可以使用ABGroupRemoveMember函数
如果想从通讯录中移除组或者联系人,可以使用ABAddressBookRemoveRecord函数
十一、操作联系人的头像
想操作联系人的头像,有以下函数
BPersonHasImageData
判断通讯录中的联系人是否有图片
ABPersonCopyImageData
取得图片数据(假如有的话)
ABPersonSetImageData
设置联系人的图片数据
设置图片数据
NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:@"lufy.jpeg"], 1);
ABPersonSetImageData(person, (__bridge CFDataRef)(data), NULL);
获得图片数据
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 #import <AddressBook/AddressBook.h> 11 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 #import <AddressBook/AddressBook.h> 11 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<count; i++) { 70 // 获取一条通讯录记录(一个联系人\一个组) 71 ABRecordRef person = CFArrayGetValueAtIndex(persons, i); 72 73 // 设置头像 74 ABPersonSetImageData(person, (__bridge CFDataRef)data, NULL); 75 } 76 77 ABAddressBookSave(book, NULL); 78 79 // 释放资源 80 CFRelease(book); 81 CFRelease(persons); 82 } 83 84 #pragma mark - 添加含有联系人的组 85 - (void)addGroup2 86 { 87 // 1.获得通讯录实例 88 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); 89 90 /* 91 2.添加一个新的组(包含新人) 92 */ 93 ABRecordRef group = ABGroupCreate(); 94 // 设置组名 95 ABRecordSetValue(group, kABGroupNameProperty, @"小学同学5", NULL); 96 ABAddressBookAddRecord(book, group, NULL); 97 ABAddressBookSave(book, NULL); 98 99 /* 100 3.添加一个新的人 101 */ 102 ABRecordRef person = ABPersonCreate(); 103 ABRecordSetValue(person, kABPersonLastNameProperty, @"李5", NULL); 104 ABRecordSetValue(person, kABPersonFirstNameProperty, @"lili5", NULL); 105 ABAddressBookAddRecord(book, person, NULL); 106 ABAddressBookSave(book, NULL); 107 108 // 4.添加联系人到组中 109 ABGroupAddMember(group, person, NULL); 110 ABAddressBookSave(book, NULL); 111 112 CFRelease(person); 113 CFRelease(group); 114 CFRelease(book); 115 } 116 117 #pragma mark - 添加组 118 - (void)addGroup 119 { 120 // 1.创建一个新组 121 ABRecordRef group = ABGroupCreate(); 122 // 设置组名 123 ABRecordSetValue(group, kABGroupNameProperty, @"大学同学", NULL); 124 125 // 2.获得通讯录实例 126 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); 127 ABAddressBookAddRecord(book, group, NULL); 128 129 // 3.保存 130 ABAddressBookSave(book, NULL); 131 132 CFRelease(group); 133 CFRelease(book); 134 } 135 136 #pragma mark - 查询所有的组 137 - (void)findGroups 138 { 139 // 1.获得通讯录实例 140 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); 141 142 // 2.获取组 143 CFArrayRef groups = ABAddressBookCopyArrayOfAllGroups(book); 144 int count = CFArrayGetCount(groups); 145 146 for (int i = 0; i<count; i++) { 147 ABRecordRef group = CFArrayGetValueAtIndex(groups, i); 148 149 CFStringRef name = ABRecordCopyValue(group, kABGroupNameProperty); 150 151 152 NSLog(@"%@", name); 153 154 CFRelease(name); 155 } 156 157 158 CFRelease(groups); 159 CFRelease(book); 160 } 161 162 #pragma mark - 修改联系人(全部人的姓都变为“刘”) 163 - (void)updatePersons 164 { 165 // 1.获得通讯录实例 166 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); 167 168 // 2.获得通讯录实例中的所有联系人数组 169 CFArrayRef persons = ABAddressBookCopyArrayOfAllPeople(book); 170 171 // 3.遍历所有的联系人数据 172 int count = CFArrayGetCount(persons); 173 for (int i = 0; i<count; i++) { 174 // 获取一条通讯录记录(一个联系人\一个组) 175 ABRecordRef person = CFArrayGetValueAtIndex(persons, i); 176 177 ABRecordSetValue(person, kABPersonLastNameProperty, @"刘", NULL); 178 } 179 180 ABAddressBookSave(book, NULL); 181 182 // 释放资源 183 CFRelease(book); 184 CFRelease(persons); 185 } 186 187 #pragma mark - 添加联系人 188 - (void)addPerson 189 { 190 // 1.创建一个新的联系人 191 ABRecordRef person = ABPersonCreate(); 192 193 // 2.设置联系人的属性 194 ABRecordSetValue(person, kABPersonLastNameProperty, @"张", NULL); 195 ABRecordSetValue(person, kABPersonFirstNameProperty, @"飞", NULL); 196 197 // 3.添加联系人到通讯录中 198 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); 199 ABAddressBookAddRecord(book, person, NULL); 200 201 // 4.保存修改 202 if(ABAddressBookHasUnsavedChanges(book)) { // 如果没有未保存的修改,就保存 203 ABAddressBookSave(book, NULL); 204 } 205 206 // 5.释放资源 207 CFRelease(person); 208 CFRelease(book); 209 } 210 211 #pragma mark - 查找所有的联系人 (简单属性) 212 - (void)findAllPersons 213 { 214 // CF的内存管理原则:只要函数中包含了Create、Copy等字眼,就主要release相应的实例 215 216 // 1.获得通讯录实例 217 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); 218 219 // 2.获得通讯录实例中的所有联系人数组 220 CFArrayRef persons = ABAddressBookCopyArrayOfAllPeople(book); 221 222 // 3.遍历所有的联系人数据 223 int count = CFArrayGetCount(persons); 224 for (int i = 0; i<count; i++) { 225 // 获取一条通讯录记录(一个联系人\一个组) 226 ABRecordRef person = CFArrayGetValueAtIndex(persons, i); 227 228 // 获得用户的firstname (名) 229 CFStringRef firstaname = ABRecordCopyValue(person, kABPersonFirstNameProperty); 230 // 获得用户的lasttname (姓) 231 CFStringRef lasttaname = ABRecordCopyValue(person, kABPersonLastNameProperty); 232 233 NSLog(@"%@ %@", lasttaname, firstaname); 234 235 // 释放资源 236 CFRelease(firstaname); 237 CFRelease(lasttaname); 238 } 239 240 // 释放资源 241 CFRelease(book); 242 CFRelease(persons); 243 } 244 245 - (void)findAllPersons2 246 { 247 // 1.获得通讯录实例 248 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); 249 250 // 2.获得通讯录实例中的所有联系人数组 251 NSArray *persons = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(book); 252 253 // 3.遍历所有的联系人数据 254 int count = persons.count; 255 for (int i = 0; i<count; i++) { 256 // 获取一条通讯录记录(一个联系人\一个组) 257 ABRecordRef person = (__bridge ABRecordRef)persons[i]; 258 259 // 获得用户的firstname (名) 260 NSString *firstaname = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); 261 // 获得用户的lasttname (姓) 262 NSString *lasttaname = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); 263 264 NSLog(@"%@ %@", lasttaname, firstaname); 265 } 266 267 // 释放资源 268 CFRelease(book); 269 } 270 271 #pragma mark 复杂属性 272 - (void)findAllPersons3 273 { 274 // 1.获得通讯录实例 275 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); 276 277 // 2.获得通讯录实例中的所有联系人数组 278 CFArrayRef persons = ABAddressBookCopyArrayOfAllPeople(book); 279 280 // 3.遍历所有的联系人数据 281 int count = CFArrayGetCount(persons); 282 for (int i = 0; i<count; i++) { 283 // 获取一条通讯录记录(一个联系人\一个组) 284 ABRecordRef person = CFArrayGetValueAtIndex(persons, i); 285 286 // 获得所有的地址 287 ABMultiValueRef addresses = ABRecordCopyValue(person, kABPersonAddressProperty); 288 // 获得第0个地址 289 CFDictionaryRef address0 = ABMultiValueCopyValueAtIndex(addresses, 0); 290 // 获得第0个地址的国家 291 CFStringRef conutry = CFDictionaryGetValue(address0, kABPersonAddressCountryKey); 292 NSLog(@"%@", conutry); 293 294 // 获得邮件 295 ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty); 296 297 // 获得邮箱类型 298 CFStringRef label0 = ABMultiValueCopyLabelAtIndex(email, 0); 299 CFStringRef label1 = ABMultiValueCopyLabelAtIndex(email, 1); 300 // 获得本地化标签 301 CFStringRef newLable0 = ABAddressBookCopyLocalizedLabel(label0); 302 CFStringRef newLable1 = ABAddressBookCopyLocalizedLabel(label1); 303 // 获得邮箱地址值 304 CFStringRef value0 = ABMultiValueCopyValueAtIndex(email, 0); 305 CFStringRef value1 = ABMultiValueCopyValueAtIndex(email, 1); 306 307 NSLog(@"%@ - %@", newLable0, value0); 308 NSLog(@"%@ - %@", newLable1, value1); 309 310 // CFRelease(label0); 311 // CFRelease(label1); 312 // CFRelease(newLable0); 313 // CFRelease(newLable1); 314 // CFRelease(value0); 315 // CFRelease(value1); 316 } 317 318 // 释放资源 319 CFRelease(book); 320 CFRelease(persons); 321 } 322 @end