IOS 获取通讯录

1.设置 info.plist 的Privacy - Contacts Usage Description

2.导入头文件

#import <Contacts/Contacts.h>

3.通过代码获取

//请求通讯录权限#pragmamark 请求通讯录权限

- (void)requestContactAuthorAfterSystemVersion9{


    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];

    if (status == CNAuthorizationStatusNotDetermined) {

        CNContactStore *store = [[CNContactStore alloc] init];

        [storerequestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError*  _Nullable error) {

            if(error) {

                NSLog(@"授权失败");

            }else{

                NSLog(@"成功授权");

            }

        }];

    }

    else if(status == CNAuthorizationStatusRestricted)

    {

        NSLog(@"用户拒绝");

        [self showAlertViewAboutNotAuthorAccessContact];

    }

    else if (status == CNAuthorizationStatusDenied)

    {

        NSLog(@"用户拒绝");

        [self showAlertViewAboutNotAuthorAccessContact];

    }

    else if (status == CNAuthorizationStatusAuthorized)//已经授权

    {

        //有通讯录权限-- 进行下一步操作

        [selfopenContact];

    }


}

//有通讯录权限-- 进行下一步操作

- (void)openContact{

 // 获取指定的字段,并不是要获取所有字段,需要指定具体的字段

    NSArray *keysToFetch = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];

    CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch];

    CNContactStore*contactStore = [[CNContactStorealloc]init];


    [contactStoreenumerateContactsWithFetchRequest:fetchRequesterror:nilusingBlock:^(CNContact*_Nonnullcontact,BOOL*_Nonnullstop) {

        NSLog(@"-------------------------------------------------------");


        NSString*givenName = contact.givenName;

        NSString*familyName = contact.familyName;

          NSLog(@"givenName=%@, familyName=%@", givenName, familyName);

        //拼接姓名

        NSString*nameStr = [NSStringstringWithFormat:@"%@%@",contact.familyName,contact.givenName];


        NSArray*phoneNumbers = contact.phoneNumbers;


        //        CNPhoneNumber  * cnphoneNumber = contact.phoneNumbers[0];


        //        NSString * phoneNumber = cnphoneNumber.stringValue;


        for(CNLabeledValue*labelValueinphoneNumbers) {

        //遍历一个人名下的多个电话号码

                NSString*label = labelValue.label;

         //  NSString *    phoneNumber = labelValue.value;

            CNPhoneNumber*phoneNumber = labelValue.value;


            NSString* string = phoneNumber.stringValue;


            //去掉电话中的特殊字符

            string = [stringstringByReplacingOccurrencesOfString:@"+86" withString:@""];

            string = [stringstringByReplacingOccurrencesOfString:@"-" withString:@""];

            string = [stringstringByReplacingOccurrencesOfString:@"(" withString:@""];

            string = [stringstringByReplacingOccurrencesOfString:@")" withString:@""];

            string = [stringstringByReplacingOccurrencesOfString:@" " withString:@""];

            string = [stringstringByReplacingOccurrencesOfString:@" " withString:@""];


        NSLog(@"姓名=%@, 电话号码是=%@", nameStr, string);




        }


        //    *stop = YES; // 停止循环,相当于break;


    }];

}

//提示没有通讯录权限

- (void)showAlertViewAboutNotAuthorAccessContact{


    UIAlertController *alertController = [UIAlertController

        alertControllerWithTitle:@"请授权通讯录权限"

        message:@"请在iPhone的\"设置-隐私-通讯录\"选项中,允许花解解访问你的通讯录"

        preferredStyle: UIAlertControllerStyleAlert];

    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];

    [alertControlleraddAction:OKAction];

    [self presentViewController:alertController animated:YES completion:nil];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 获取手机通讯录导入Contacts/Contacts 这个库, 不带UI 导入ContactsUI/Contact...
    冬日的太阳_c107阅读 1,113评论 0 0
  • day12——面向对象 一、上周知识点回顾 1、分支、循环语句 if语句,switch语句 for语句,while...
    机会留给有准备的人阅读 243评论 0 0
  • 每天在家里,大门不出二门不迈,手机都是放屋子里,天黑一看运动步数,有时候一百不到。这个时代,还真应了我这种懒...
    简单去爱静待花开阅读 131评论 0 1
  • Flask打造个人博客系列,今天是第一篇,我们详细讲解登录,注册模块。 既然是Flask来开发,你得: 安装 实现...
    Thinkbigger阅读 1,556评论 2 50
  • 昨晚1:30睡觉,今天10:00起床。作息在向着好的方向发展,重要的是尽量不要睡午觉,否则又是新一轮的凌晨众人皆醉...
    d11110d0172b阅读 84评论 0 0