IOS之通讯录开发(附Demo)

作者: 辛小二 | 来源:发表于2017-09-06 13:37 被阅读406次

目录

 1、集成
 2、Xcode8.0之后的权限添加(否则上架不允许)
 3、通讯录数据获取
 4、通讯录数据排序
 5、searchBar实现模糊查询
 6、小节

1、集成

#import <ContactsUI/ContactsUI.h>     头文件的导入

2、Xcode8.0之后的限定(否则上架不允许)

info.plist
 Privacy - Contacts Usage Description -> 通讯录权限

3、通讯录数据获取

- (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;  // 停止循环,相当于break;
    }];
    NSLog(@"%ld",self.contactsSource.count);
    NSLog(@"%@",self.contactsSource);
    //根据Person对象的 name 属性 按中文 对 Person数组 排序
    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我们是通过再遍历数组取到的。
(3)那么取到之后我们将这两个对象分别放在字典当中,再将字典放在数组当中,从而实现这样一个集合(self.contactsSource).
(4)我们又将这个集合通过转换model的形式转换为了Model类型,从而实现在cell上的赋值。
通过以上四步我们实现了对通讯录数组的赋值---效果如图

效果图

4、通讯录数据排序

1、说到排序,我们又重新创建了两个数组
//排序后的出现过的拼音首字母数组
@property(nonatomic,strong)NSMutableArray *indexArray;
//排序好的结果数组
@property(nonatomic,strong)NSMutableArray *letterResultArr;

分别是排序好字母的数组(来作为section的头) 
按照首字母排序好的数组(来作为每个section的row)

2、获取到之后,重新按照对象-->model的形式,再次赋值到cell上即可。

5、searchBar实现模糊查询

说到searchBar这里不对SearchBar进行详解。只说几个常用的代理--

(1)首先要实现<UISearchBarDelegate,UISearchDisplayDelegate>两个协议

(2)实现代理方法

- (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];
//    /**通过谓词修饰的方式来查找包含我们搜索关键字的数据*/
    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) 最重要的还是要实现UISearchDisplayDelegate中的代理方法我们需要创建一个UISearchDisplayController的控制器。

@property (nonatomic, strong) UISearchDisplayController *displayer;

(4)然后我们我们需要看下,在UISearchDisplayDelegate的代理中我们具体实现了什么,这里我们使用了NSPredicate(作为筛选工具 苹果自带的 )想看详情点这里传送门
(5)我们将筛选过后的数组还是按照. 对象转模型(model)的方式再次赋值到Cell上完成这波操作。。效果图如下。

效果图

小结----- >

通讯录Demo

代码都是基本操作,希望能帮到你。喜欢的话请点赞。。。

相关文章

网友评论

    本文标题:IOS之通讯录开发(附Demo)

    本文链接:https://www.haomeiwen.com/subject/vdvmjxtx.html