/**
获取通讯录
**/
-(void)getAddressBookList{
NSMutableArray *array = [NSMutableArray array];//接受所有的人信息
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
//请求通讯录权限
ABAddressBookRequestAccessWithCompletion(book, ^(bool granted, CFErrorRef error) {
//把所有的联系人复制到数组中
NSArray *peopleArray = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(book);
for (int i = 0; i < peopleArray.count; i++) {
ContactsModel *model = [[ContactsModel alloc]init];//每个人数据模型
ABRecordRef person = (__bridge ABRecordRef)([peopleArray objectAtIndex:i]);
//获得名字
NSString *name = (__bridge NSString *)ABRecordCopyCompositeName(person);
model.name = name;
if (name) {
//获得电话号码
ABMultiValueRef tmpPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
if (tmpPhones!=nil&&(__bridge NSObject*)tmpPhones!=[NSNull null]) {
NSMutableArray *phoneArray = [NSMutableArray array];
for(NSInteger j = 0; j < ABMultiValueGetCount(tmpPhones); j++){
NSString *tmpPhoneIndex = (__bridge NSString*)ABMultiValueCopyValueAtIndex(tmpPhones, j);
NSString *phone = [self formatPhoneNum:tmpPhoneIndex];
[phoneArray addObject:phone];//添加多个号码
// ABMultiValueRef ref = ABRecordCopyValue(person, kABPersonPhoneProperty);
// //获得昵称
// NSString* tmpNickname = (__bridge NSString)ABRecordCopyValue(person, kABPersonNicknameProperty);
// //获取的联系人单一属性:公司名字
// NSString tmpCompanyname = (__bridge NSString)ABRecordCopyValue(person, kABPersonOrganizationProperty);
// //获取的联系人单一属性:邮箱
// NSString tmpEmail = (__bridge NSString*)ABMultiValueCopyValueAtIndex(ref, 0);
}
model.phonesArray = phoneArray;
}
}
[array addObject:model];
}
//处理数据 数组装载模型
[self handleContactsDataWithAddressBookList:array];
});
}
//将通讯录分组装进字典处理展示
-
(void)handleContactsDataWithAddressBookList:(NSMutableArray *)ContactModels{//分组装进字典
NSMutableDictionary *allDataDic = [NSMutableDictionary dictionary];
NSMutableArray *array = [NSMutableArray arrayWithArray:ContactModels];for (int i = 0; i < array.count; i ++) {
ContactsModel *model = array[i]; NSMutableString *key =(NSMutableString *)[[[self changeToPinYinWithString:model.name] substringToIndex:1] uppercaseString];//取首字母大写 NSMutableArray *tempArray = [@[] mutableCopy]; [tempArray addObject:model]; for (int j = i+1; j < array.count; j ++) { ContactsModel *model2 = array[j]; NSMutableString *key2 = (NSMutableString *)[[[self changeToPinYinWithString:model2.name] substringToIndex:1] uppercaseString];//取首字母大写 if([key2 isEqualToString:key]){//将首字母相同的加载到同一个数组 [tempArray addObject:model2]; [array removeObjectAtIndex:j]; j=j-1; } } [allDataDic setObject:tempArray forKey:key];
}
}
//将传过来的字符串转成拼音
(NSMutableString *)changeToPinYinWithString:(NSString *)str{
NSMutableString *tempStr = [[NSMutableString alloc]init];
if (str) {
NSMutableString *ms = [[NSMutableString alloc] initWithString:str];
if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformMandarinLatin, NO)) {
// NSLog(@"pinyin: %@", ms);//带音调的转拼音
}
if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO)) {
// NSLog(@"pinyin: %@", ms);//转拼音
tempStr = (NSMutableString *)ms;
}
}
return tempStr;
}
//对通讯录取出的号码进行处理-
(NSString *)formatPhoneNum:(NSString *)phone
{if ([phone hasPrefix:@"86"]) {
NSString *formatStr = [phone substringWithRange:NSMakeRange(2, [phone length]-2)]; return formatStr;
} else if ([phone hasPrefix:@"+86"]){
NSString *formatStr = [phone substringWithRange:NSMakeRange(4, [phone length]-4)]; return formatStr;
} else if ([phone hasPrefix:@"00 86"]){
NSString *formatStr = [phone substringWithRange:NSMakeRange(6, [phone length]-6)]; return formatStr;
}
if ([phone contains:@"-"]) { phone = [phone stringByReplacingOccurrencesOfString:@"-" withString:@""]; } if ([phone contains:@" "]) { phone = [phone stringByReplacingOccurrencesOfString:@" " withString:@""]; }
return phone;
}