iOS获取通讯录信息

    xiaoxiao2021-03-25  101

    #pragma 获取通讯录权限

    -(void)getPhoneName;

    {

        //让用户给权限,没有的话会被拒的各位

        CNAuthorizationStatus status = [CNContactStoreauthorizationStatusForEntityType:CNEntityTypeContacts];

        if (status ==CNAuthorizationStatusNotDetermined) {

            CNContactStore *store = [[CNContactStorealloc] init];

            [store requestAccessForEntityType:CNEntityTypeContactscompletionHandler:^(BOOL granted,NSError * _Nullable error) {

                if (error) {

                    NSLog(@"用户拒绝授权");

                  

                }else

                {

                    NSLog(@"授权成功");//用户给权限了,就去获取通讯录信息

                    [selfgetPhoneWithStore:store];

                }

            }];

        }

        

        if (status ==CNAuthorizationStatusAuthorized) {//有权限时,获取通讯录信息

            CNContactStore *store = [[CNContactStorealloc] init];

            [selfgetPhoneWithStore:store];

        }

        else{

            NSLog(@"您未开启通讯录权限,请前往设置中心开启");

            

            

        }

    }

    #pragma 获取通讯录信息

    -(void)getPhoneWithStore:(CNContactStore*)store;

    {

        // 3.2.创建联系人的请求对象

        // keys决定这次要获取哪些信息,比如姓名/电话

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

        CNContactFetchRequest *request = [[CNContactFetchRequestalloc] initWithKeysToFetch:fetchKeys];

        

        // 3.3.请求联系人

        NSError *error =nil;

        [store enumerateContactsWithFetchRequest:requesterror:&error usingBlock:^(CNContact *_Nonnull contact, BOOL * _Nonnull stop) {

            // stop是决定是否要停止

            // 1.获取姓名

            NSString *firstname = contact.givenName;

            NSString *lastname = contact.familyName;

            NSLog(@"%@ %@", firstname, lastname);

            

            // 2.获取电话号码

            NSArray *phones = contact.phoneNumbers;

            

            // 3.遍历电话号码

            for (CNLabeledValue *labelValuein phones) {

                CNPhoneNumber *phoneNumber = labelValue.value;

                NSLog(@"%@ %@", phoneNumber.stringValue, labelValue.label);

            }

        }];

    }

    ————————————————————————————————————跳转到通讯录界面—————————————————————————————————

    一、导入头文件

    #import <Contacts/Contacts.h>

    #import <ContactsUI/ContactsUI.h>

    二、遵循协议

    <CNContactPickerDelegate>

    三、跳转到通讯录界面

    // 1.创建选择联系人的控制器

        CNContactPickerViewController *contactVc = [[CNContactPickerViewController allocinit];

        // 2.设置代理

        contactVc.delegate = self;

        // 3.弹出控制器

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

    四、实现代理方法

    // 1.点击取消按钮调用的方法

    - (void)contactPickerDidCancel:(CNContactPickerViewController *)picker

    {

        DLog(@"取消选择联系人");

    }

    // 2.当选中某一个联系人时会执行该方法

    - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact

    {

        // 1.获取联系人的姓名

        NSString *lastname = contact.familyName;

        NSString *firstname = contact.givenName;

        DLog(@"%@ %@", lastname, firstname);

        

        // 2.获取联系人的电话号码(此处获取的是该联系人的第一个号码,也可以遍历所有的号码)

        NSArray *phoneNums = contact.phoneNumbers;

        CNLabeledValue *labeledValue = phoneNums[0];

        CNPhoneNumber *phoneNumer = labeledValue.value;

        NSString *phoneNumber = phoneNumer.stringValue;

        DLog(@"%@", phoneNumber);

    }

    转载请注明原文地址: https://ju.6miu.com/read-18882.html

    最新回复(0)