iOS通讯录开发

前言

记录下在应用内获取通讯录的相关知识点,涉及到通讯录权限配置,通讯录数据的获取以及数据按首字母排序。

系统通讯录权限的获取

  1. iOS关于权限这一块不需要多讲,访问位置信息,麦克风,通讯这些都需要权限设置,这里就贴一下通讯录的,Info plist文件中添加:

key:Privacy - Contacts Usage Descriptio

value:是否允许(应用名)访问您的通讯录?

2.对于通讯录权限的处理

#import

- (void)requestContactAuthorAfterSystem{
    
    //获取当前的授权状态
    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    
    if (status == CNAuthorizationStatusNotDetermined) {
        //用户还没有就应用程序是否可以访问联系人数据做出选择。
        //请求弹窗选择
        CNContactStore *store = [[CNContactStore alloc] init];
        [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError*  _Nullable error) {
            if (error) {
                NSLog(@"授权失败");
            }else {
               
                //成功授权
                //-----todo----
            }
        }];
    }
    else if(status == CNAuthorizationStatusRestricted)
    {
        //用户没有权限访问
    }
    else if (status == CNAuthorizationStatusDenied)
    {
        //用户已拒绝,引导用户开启
        UIAlertController *alertController = [UIAlertController
            alertControllerWithTitle:@"请授权通讯录权限"
            message:@"请在iPhone的\"设置-隐私-通讯录\"选项中,允许(应用名)访问你的通讯录"
            preferredStyle: UIAlertControllerStyleAlert];

        UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:OKAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    else if (status == CNAuthorizationStatusAuthorized)//已经授权
    {
        //有通讯录权限
        //-----todo----
    }
    
}

读取系统通讯录的数据

在获取到通讯录权限后进行数据的请求

//获取通讯录数据
- (void)loadData {

    [SVProgressHUD showWithStatus:@"加载中"];
    // 获取指定的字段,并不是要获取所有字段,需要指定具体的字段
    //CNContactGivenNameKey联系人的名字 CNContactFamilyNameKey联系人的姓氏CNContactPhoneNumbersKey电话号码
    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) {
       
        //拼接姓名
        NSString *nameStr = [NSString stringWithFormat:@"%@%@",contact.familyName,contact.givenName];
         //获取电话号码
        NSArray *phoneNumbers = contact.phoneNumbers;
        YXAddressBookModel *model = [[YXAddressBookModel alloc] init];
        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:@""];
            string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
            NSLog(@"姓名=%@, 电话号码是=%@", nameStr, string);
        }
        [SVProgressHUD dismiss];
        /*
        ---todo---  
        数据展示刷新表视图等
        */
    }];
}

数据的处理

根据首字母对数据进行排序,使用的排序类 UILocalizedIndexedCollation

//通过首字母排序
-(NSMutableArray *)sortObjectsAccordingToInitialWith:(NSArray *)arr {
 
    // 初始化UILocalizedIndexedCollation
    UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
 
    //得出collation索引的数量,这里是27个(26个字母和1个#)
    NSInteger sectionTitlesCount = [[collation sectionTitles] count];
    //初始化一个数组newSectionsArray用来存放最终的数据,我们最终要得到的数据模型应该形如@[@[以A开头的数据数组], @[以B开头的数据数组], @[以C开头的数据数组], ... @[以#(其它)开头的数据数组]]
    NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
 
    //初始化27个空数组加入newSectionsArray
    for (NSInteger index = 0; index < sectionTitlesCount; index++) {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [newSectionsArray addObject:array];
    }
 
    //将每个名字分到某个section下
    /*
     - (NSInteger)sectionForObject:(id)object collationStringSelector:(SEL)selector;
    // 返回将包含该对象的section的索引
    // 选择器不能接受任何参数并返回一个NSString对像
    */
    //遍历数组,YXAddressBookModel
    for (YXAddressBookModel *personModel in arr) {
        //获取name属性的值所在的位置,比如"林丹",首字母是L,在A~Z中排第11(第一位是0),sectionNumber就为11
        NSInteger sectionNumber = [collation sectionForObject:personModel collationStringSelector:@selector(name)];
        //把name为“林丹”的p加入newSectionsArray中的第11个数组中去
        NSMutableArray *sectionNames = newSectionsArray[sectionNumber];
        [sectionNames addObject:personModel];
    }
 
    //对每个section中的数组按照name属性排序
    for (NSInteger index = 0; index < sectionTitlesCount; index++) {
        NSMutableArray *personArrayForSection = newSectionsArray[index];
        NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(name)];
        newSectionsArray[index] = sortedPersonArrayForSection;
    }
    
    //最后返回的应该是一个装有27个数组的数组对象
    return newSectionsArray;
}

优化

在通讯录的数据比较多的时候,可以考虑将获取到的数据保存在本地,下次进来页面时先判断本地是否有存储的数据,没有再去访问系统通讯录,这样的话记得同步本地和系统通讯录的数据。

结语

swift版本demo地址

你可能感兴趣的:(iOS通讯录开发)