iOS 10开始,读取通讯录需要添加隐私设置,不然会造成程序崩溃。
<key>NSContactsUsageDescription</key>
<string>客户端需要访问您的通讯录</string>
写两个读取通信录得工具类,一个是判断是否拥有读取通讯录权限的,一个是用来获取通讯录的
// AddressBookHelper.h
#import <Foundation/Foundation.h>
typedef void(^AddressBookReadFinishBlock)(NSArray *addressBookArray);
typedef void(^AddressBookCheckFinishBlock)(bool isAuthorized);
@interface AddressBookHelper : NSObject
//to do wangyatao 读取通讯录
+(void)getAddressBookWithAddressBookReadFinishiBlock:(AddressBookReadFinishBlock)finishBlock;
+(void)CheckAddressBookAuthorization:(AddressBookCheckFinishBlock)checkFinishBlock;
@end
// AddressBookHelper.m
#import "AddressBookHelper.h"
#import <AddressBook/AddressBook.h>
#import "Contacts/Contacts.h"
@implementation AddressBookHelper
+(void)getAddressBookWithAddressBookReadFinishiBlock:(AddressBookReadFinishBlock)finishBlock{
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
__block ABAddressBookRef addressBook = nil;
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error){
CFErrorRef *error1 = NULL;
addressBook = ABAddressBookCreateWithOptions(NULL, error1);
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){
CFErrorRef *error = NULL;
addressBook = ABAddressBookCreateWithOptions(NULL, error);
}
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
//name-phoneNumber
NSMutableArray *addressBookArr = [NSMutableArray array];
for ( int i = 0; i < numberOfPeople; i++){
//name-phoneNumber
NSDictionary *addressDic = [NSDictionary dictionary];
NSString *name = @"";
NSString *phoneNumber = @"";
ABRecordRef person = CFArrayGetValueAtIndex(people, i);
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
if (lastName.length>0) {
name = [NSString stringWithFormat:@"%@%@",name,lastName];
}
if (firstName.length>0) {
name = [NSString stringWithFormat:@"%@%@",name,firstName];
}
//读取电话多值
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (int k = 0; k<ABMultiValueGetCount(phone); k++)
{
//获取电话Label
//NSString * personPhoneLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));
//获取該Label下的电话值
NSString * personPhone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phone, k);
personPhone = [personPhone stringByReplacingOccurrencesOfString:@"-" withString:@""];
if (personPhone.length == 11) {
phoneNumber = personPhone;
continue;
}
}
if (name.length>0 && phoneNumber.length>0) {
addressDic = @{@"num":phoneNumber,@"name":name};
[addressBookArr addObject:addressDic];
}
}
if (finishBlock) {
finishBlock(addressBookArr.copy);
}
}
+(void)CheckAddressBookAuthorization:(void (^)(bool isAuthorized))block {
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus();
if (authStatus != kABAuthorizationStatusAuthorized) {
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
NSLog(@"Error: %@", (__bridge NSError *)error);
} else if (!granted) {
block(NO);
} else{
block(YES);
}
});
});
} else{
block(YES);
}
}
@end
// ViewController.m
#import "ViewController.h"
#import "AddressBookHelper.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, 40)];
[button setTitle:@"读取通讯录" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)buttonClicked{
[AddressBookHelper CheckAddressBookAuthorization:^(bool isAuthorized) {
if (isAuthorized) {
//权限获取成功
//检查是否拥有读取用户通讯录的权限,如果没有,会弹出权限申请框,但是只有第一次执行时参会弹框提示,如果用户选择不允许,以后也不会再弹框了,只能提示用户去设置中更改权限。
[AddressBookHelper getAddressBookWithAddressBookReadFinishiBlock:^(NSArray *addressBookArray) {
NSMutableString *stringM = [NSMutableString string];
for (NSDictionary *dic in addressBookArray) {
NSString *person = [NSString stringWithFormat:@"{\\\"name\\\":\\\"%@\\\",\\\"num\\\":\\\"%@\\\"},",dic[@"name"],dic[@"num"]];
[stringM appendString:person];
}
NSLog(@"%@",stringM);
}];
}else{
//权限获取失败
[self alertMessage:@"程序需要获取通讯录权限,请到设置中进行设置。" andTitle:@"提示"];
}
}];
}
//弹出提示信息
-(void)alertMessage:(NSString *)message andTitle:(NSString *)title{
UIAlertController *ac = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *aa = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[ac addAction:aa];
[self presentViewController:ac animated:YES completion:nil];
}
@end
更新 获取通讯录的一个坑
获取通讯录中用户全名的时候, 出现一个问题, 部分手机获取通讯录不全. 有一部分用户的手机号获取不到. 而且测试提出问题, 开发这里也无法复现.
现在分析问题发现, 任何客服端无法复现的bug, 都是因为没有找到问题根本的原因. 当然了, 如果找到了问题的根本原因, 可以完美复现bug了, 问题也就解决百分之八十了.
然后事实是测试人员发现bug就兴奋的提到bugfree, 根本不会给你总结bug复现规律的. 所以一切还得靠自己.
问题描述: 客户端读取本地通讯录, 读取后填充到页面显示. 发现部分测试机总有几个手机号读取不到. 比如王小明的手机号读取不到. 然后我尝试添加王小明到我的手机通讯录上, 可以读取到.
误区: 开始一直以为是代码中某些逻辑过滤掉了 某些手机号码. 所以查了好久也没有查到问题, 而且如上所说, 如果是过滤掉的, 那应该在其他手机可以复现, 结果并没有复现.
问题原因: 代码使用了 CNContactStore 方法获取通讯录(ios9+), 代码如下:
[store enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact,BOOL * _Nonnull stop) {
// 获取联系人全名
NSString *name = [CNContactFormatter stringFromContact:contact style:CNContactFormatterStyleFullName];
// 获取一个人的所有电话号码
NSArray *phones = contact.phoneNumbers;
for (CNLabeledValue *labelValue in phones) {
CNPhoneNumber *phoneNumber = labelValue.value;
NSString *mobile = [self removeSpecialSubString:phoneNumber.stringValue];
NSDictionary *addressDic = [NSDictionary dictionary];
addressDic = @{@"num":mobile,@"name":name};
[addressBookArr addObject:addressDic];
}
}];
注意代码中, 获取用户全名, 我使用了NSString *name = [CNContactFormatter stringFromContact:contact style:CNContactFormatterStyleFullName];
方法, 而该方法有一个问题是, 当通讯录中存在一个用户名为空的记录时, 全名会变成nil, 使得遍历过程中断(但是不会使程序崩溃). 中断后, 未遍历到的用户便成了"丢失人口".
发现这个原因后, 复现了bug, 更加坚信这是问题的原因. 我在自己手机通讯录中添加一条用户名为空的记录(通讯录中添加一个手机号, 然后不添加名字就保存). 然后再在通讯录中添加几个正常的记录, 再读取通讯录的过程中发现, 在用户名为空的记录后边添加的所有记录都读取不到. 很有规律, 猜测ios遍历通讯录是按照通讯录的添加时间的顺序, 而非人名的首字母.
解决方法: 更改获取通讯录全名的方法:
// 获取联系人全名
//NSString *name = [CNContactFormatter stringFromContact:contact style:CNContactFormatterStyleFullName];
//20180515说明:不再使用CNContactFormatterStyleFullName获取全名,这种情况下, 如果通讯录中存在只有号码没有名字的情况,会被(null)错误终止遍历, 导致通讯录读取不全.
NSString * givenName = contact.givenName;
NSString * familyName = contact.familyName;
NSLog(@"%@--%@", givenName, familyName);
NSString *name = [NSString stringWithFormat:@"%@%@",familyName, givenName];
完美!!
网友评论