iOS自定義通訊錄
最近開發一個關於社交的App需要獲取通訊錄的資訊,由於專案由iOS9開始相容,所以使用的時候iOS9以後蘋果新推出的框架<Contacts/Contacts.h>
獲取通訊錄資訊列表
1.獲取許可權
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]; if (status == CNAuthorizationStatusNotDetermined) { CNContactStore *contactStore = [[CNContactStore alloc]init]; [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { if (!granted) { NSLog(@"授權失敗"); }else{ NSLog(@"成功授權"); [self getContactInfo]; //讀取資料 } }]; }else if (status == CNAuthorizationStatusRestricted){ NSLog(@"使用者拒絕授權"); }else if (status == CNAuthorizationStatusAuthorized){//已經授權 [self getContactInfo];//讀取資料 }
2.讀取資料
NSArray *keysToFetch = @[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactPhoneNumbersKey]; //一個聯絡人可能存在多個電話和暱稱 CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc]initWithKeysToFetch:keysToFetch]; CNContactStore *store = [CNContactStore new]; [store enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) { ContacterModel *model = [ContacterModel new]; NSString *giveName = contact.givenName; NSString *familyName = contact.familyName; //拼接姓名 NSString *nameStr = [NSString stringWithFormat:@"%@%@",familyName,giveName]; model.name = nameStr; NSArray *phoneNumbers = contact.phoneNumbers; for (CNLabeledValue *labelValue in phoneNumbers) { //遍歷一個人名下的多個電話號碼 CNPhoneNumber *phoneNumber = labelValue.value; NSString *string = phoneNumber.stringValue; //去除特殊字元 string = [string stringByReplacingOccurrencesOfString:@"+86" withString:@""]; string = [string stringByReplacingOccurrencesOfString:@"-" withString:@""]; string = [string stringByReplacingOccurrencesOfString:@"(" withString:@""]; string = [string stringByReplacingOccurrencesOfString:@")" withString:@""]; string = [string stringByReplacingOccurrencesOfString:@" " withString:@""]; [model.numberArray addObject:string]; } if (![model.name isEqualToString:@""] && model.numberArray.count >0 ) { [self.dataArray addObject:model]; } }];
3.資料排序分組
self.sortedDic = [NSMutableDictionary dictionary]; [self.dataArray enumerateObjectsUsingBlock:^(id_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { ContacterModel* model = (ContacterModel *)obj; NSString *nameIndex = model.nameIndex; if (!self.sortedDic[nameIndex]) { //如果不存在這個欄位,則建立 [self.sortedDic setValue:[NSMutableArray array] forKey:nameIndex]; } [self.sortedDic[nameIndex] addObject:model]; }]; //將字典中的key排序:A-Z self.keyArray = [[_sortedDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
關於聯絡人我使用了一個模型來定義 其中有一個屬性是:@property(nonatomic,copy)NSString *nameIndex;
這個屬性定義的是聯絡人名稱拼音的首字母
獲取姓名拼音的首字母
+(NSString*)getFistLetterFromString:(NSString*)aString{ if (!aString || [aString isEqualToString:@""]) { return @"#"; } NSMutableString *mutableString = [NSMutableString stringWithString:aString]; CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformMandarinLatin, false);//轉換為拼音 CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformStripDiacritics, false);//去除音標 NSString *pinyinString = [mutableString uppercaseString]; //這裡可以考慮多音字的處理(暫未實現) NSString *firstStr = [pinyinString substringToIndex:1]; //判斷姓名首位是否為大寫字母 NSString *regxA = @"^[A-Z]$"; NSPredicate *preA = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regxA]; return [preA evaluateWithObject:firstStr] ? firstStr : @"#";//如果首位不是大寫字母開頭就設定為# }
備註
其中獲取名字的首字母和資料的分組排序是知識點,部分Api使用較少,想要深入理解的請自行百度
ofollow,noindex">Demo下載