目錄
1郎汪、集成
2、Xcode8.0之后的權限添加(否則上架不允許)
3烈拒、通訊錄數(shù)據獲取
4乳乌、通訊錄數(shù)據排序
5器躏、searchBar實現(xiàn)模糊查詢
6、小節(jié)
1、集成
#import <ContactsUI/ContactsUI.h> 頭文件的導入
2、Xcode8.0之后的限定(否則上架不允許)
Privacy - Contacts Usage Description -> 通訊錄權限
3磺平、通訊錄數(shù)據獲取
- (void)contactsUI{
CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (authorizationStatus == CNAuthorizationStatusAuthorized) {
NSLog(@"沒有授權...");
}
// 獲取指定的字段,并不是要獲取所有字段,需要指定具體的字段
NSArray *keysToFetch = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch];
CNContactStore *contactStore = [[CNContactStore alloc] init];
[contactStore enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
self.contactsDic = [NSMutableDictionary dictionary];
NSLog(@"-------------------------------------------------------");
NSString *givenName = contact.givenName;
NSString *familyName = contact.familyName;
NSLog(@"givenName=%@, familyName=%@", givenName, familyName);
[self.contactsDic setObject:familyName forKey:@"familName"];
NSArray *phoneNumbers = contact.phoneNumbers;
for (CNLabeledValue *labelValue in phoneNumbers) {
NSString *label = labelValue.label;
CNPhoneNumber *phoneNumber = labelValue.value;
[self.contactsDic setObject:phoneNumber.stringValue forKey:@"phoneNumber"];
NSLog(@"label=%@, phone=%@", label, phoneNumber.stringValue);
}
NSLog(@"%@",self.contactsDic);
[self.contactsSource addObject:self.contactsDic];
// *stop = YES; // 停止循環(huán)拐辽,相當于break拣挪;
}];
NSLog(@"%ld",self.contactsSource.count);
NSLog(@"%@",self.contactsSource);
//根據Person對象的 name 屬性 按中文 對 Person數(shù)組 排序
self.indexArray = [BMChineseSort IndexWithArray:self.contactsSource Key:@"familName"];
NSLog(@"%@",self.indexArray);
for (int i = 0; i < self.contactsSource.count; i++) {
constantModel *model = [[constantModel alloc] initWithDict:self.contactsSource[i]];
[self.contactsSourceList addObject:model];
}
self.letterResultArr = [BMChineseSort sortObjectArray:self.contactsSourceList Key:@"familName"];
NSLog(@"%@",self.letterResultArr);
[self.mainTableView reloadData];
}
(1)上面的代碼我們可以看到---Block回調當中的contact下會有很多屬性(我這里只抓取了familyName和phoneNumber 也就是昵稱和電話).
(2)familyName直接就可以"."出來。而phoneNumber我們是通過再遍歷數(shù)組取到的俱诸。
(3)那么取到之后我們將這兩個對象分別放在字典當中菠劝,再將字典放在數(shù)組當中,從而實現(xiàn)這樣一個集合(self.contactsSource).
(4)我們又將這個集合通過轉換model的形式轉換為了Model類型睁搭,從而實現(xiàn)在cell上的賦值赶诊。
通過以上四步我們實現(xiàn)了對通訊錄數(shù)組的賦值---效果如圖
4、通訊錄數(shù)據排序
1介袜、說到排序甫何,我們又重新創(chuàng)建了兩個數(shù)組
//排序后的出現(xiàn)過的拼音首字母數(shù)組
@property(nonatomic,strong)NSMutableArray *indexArray;
//排序好的結果數(shù)組
@property(nonatomic,strong)NSMutableArray *letterResultArr;
分別是排序好字母的數(shù)組(來作為section的頭)
按照首字母排序好的數(shù)組(來作為每個section的row)
2出吹、獲取到之后遇伞,重新按照對象-->model的形式,再次賦值到cell上即可捶牢。
5鸠珠、searchBar實現(xiàn)模糊查詢
說到searchBar這里不對SearchBar進行詳解巍耗。只說幾個常用的代理--
(1)首先要實現(xiàn)<UISearchBarDelegate,UISearchDisplayDelegate>兩個協(xié)議
(2)實現(xiàn)代理方法
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
NSLog(@"begin");
return YES;
}
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
NSLog(@"end");
return YES;
}
#pragma mark ----------------UISearchDisplayDelegate---------------------
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
NSLog(@"%@",self.contactsSource);
self.searchModelResultArray = [NSMutableArray array];
// /**通過謂詞修飾的方式來查找包含我們搜索關鍵字的數(shù)據*/
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.familName CONTAINS[cd] %@",searchString];
NSLog(@"%@",predicate);
self.searchResultArray = [[self.contactsSource filteredArrayUsingPredicate:predicate]mutableCopy];
NSLog(@"%@",self.searchResultArray);
for (int i = 0; i < self.searchResultArray.count; i++) {
constantModel *model = [[constantModel alloc] initWithDict:self.searchResultArray[i]];
[self.searchModelResultArray addObject:model];
}
NSLog(@"%@",self.searchModelResultArray);
return YES;
}
(3) 最重要的還是要實現(xiàn)UISearchDisplayDelegate中的代理方法我們需要創(chuàng)建一個UISearchDisplayController的控制器。
@property (nonatomic, strong) UISearchDisplayController *displayer;
(4)然后我們我們需要看下渐排,在UISearchDisplayDelegate的代理中我們具體實現(xiàn)了什么炬太,這里我們使用了NSPredicate(作為篩選工具 蘋果自帶的 )想看詳情點這里傳送門
(5)我們將篩選過后的數(shù)組還是按照. 對象轉模型(model)的方式再次賦值到Cell上完成這波操作。驯耻。效果圖如下亲族。
小結----- >
通訊錄Demo
代碼都是基本操作,希望能幫到你可缚。喜歡的話請點贊霎迫。。帘靡。