/**
獲取通訊錄
**/
-(void)getAddressBookList{
NSMutableArray *array = [NSMutableArray array];//接受所有的人信息
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
//請(qǐng)求通訊錄權(quán)限
ABAddressBookRequestAccessWithCompletion(book, ^(bool granted, CFErrorRef error) {
//把所有的聯(lián)系人復(fù)制到數(shù)組中
NSArray *peopleArray = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(book);
for (int i = 0; i < peopleArray.count; i++) {
ContactsModel *model = [[ContactsModel alloc]init];//每個(gè)人數(shù)據(jù)模型
ABRecordRef person = (__bridge ABRecordRef)([peopleArray objectAtIndex:i]);
//獲得名字
NSString *name = (__bridge NSString *)ABRecordCopyCompositeName(person);
model.name = name;
if (name) {
//獲得電話號(hào)碼
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];//添加多個(gè)號(hào)碼
// ABMultiValueRef ref = ABRecordCopyValue(person, kABPersonPhoneProperty);
// //獲得昵稱
// NSString* tmpNickname = (__bridge NSString)ABRecordCopyValue(person, kABPersonNicknameProperty);
// //獲取的聯(lián)系人單一屬性:公司名字
// NSString tmpCompanyname = (__bridge NSString)ABRecordCopyValue(person, kABPersonOrganizationProperty);
// //獲取的聯(lián)系人單一屬性:郵箱
// NSString tmpEmail = (__bridge NSString*)ABMultiValueCopyValueAtIndex(ref, 0);
}
model.phonesArray = phoneArray;
}
}
[array addObject:model];
}
//處理數(shù)據(jù) 數(shù)組裝載模型
[self handleContactsDataWithAddressBookList:array];
});
}
//將通訊錄分組裝進(jìn)字典處理展示
-
(void)handleContactsDataWithAddressBookList:(NSMutableArray *)ContactModels{//分組裝進(jìn)字典
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]){//將首字母相同的加載到同一個(gè)數(shù)組 [tempArray addObject:model2]; [array removeObjectAtIndex:j]; j=j-1; } } [allDataDic setObject:tempArray forKey:key];
}
}
//將傳過來的字符串轉(zhuǎn)成拼音
(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);//帶音調(diào)的轉(zhuǎn)拼音
}
if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO)) {
// NSLog(@"pinyin: %@", ms);//轉(zhuǎn)拼音
tempStr = (NSMutableString *)ms;
}
}
return tempStr;
}
//對(duì)通訊錄取出的號(hào)碼進(jìn)行處理-
(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;
}