美文网首页
iOS 获取手机通讯录

iOS 获取手机通讯录

作者: 冬日的太阳_c107 | 来源:发表于2020-02-17 17:04 被阅读0次

    获取手机通讯录导入Contacts/Contacts  这个库, 不带UI  导入ContactsUI/ContactsUI 这个库, 带UI 

    直接上代码把

    //带UI(无代理)

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{   CNContactPickerViewController * contactPickerVc = [CNContactPickerViewController new];

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

    }

    ui效果

    //带UI(有代理)

    效果图

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{   CNContactPickerViewController * contactPickerVc = [CNContactPickerViewController new];

       contactPickerVc.delegate=self;

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

    }

    #pragma mark - 选中一个联系人

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

        NSLog(@"contact:%@",contact);

        //phoneNumbers 包含手机号和家庭电话等

        for(CNLabeledValue* labeledValueincontact.phoneNumbers) {

            CNPhoneNumber* phoneNumber = labeledValue.value;

            NSLog(@"phoneNum:%@", phoneNumber.stringValue);

             }

    }

    #pragma mark - 选中一个联系人属性

    - (void)contactPicker:(CNContactPickerViewController*)picker didSelectContactProperty:(CNContactProperty*)contactProperty{

        NSLog(@"contactProperty:%@",contactProperty);

    }

    #pragma mark - 选中一个联系人的多个属性

    - (void)contactPicker:(CNContactPickerViewController*)picker didSelectContactProperties:(NSArray*)contactProperties{

        NSLog(@"contactPropertiescontactProperties:%@",contactProperties);

    }

    #pragma mark - 选中多个联系人

    - (void)contactPicker:(CNContactPickerViewController*)picker didSelectContacts:(NSArray*)contacts{

        NSLog(@"contactscontacts:%@",contacts);

    }

    //不带UI

    //请求通讯录权限

    #pragma mark 请求通讯录权限

    - (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 = [[CNContactStore alloc] 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*phoneNumber1 = labelValue.value;

                NSString* string = phoneNumber1.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;

        }];

    }

    相关文章

      网友评论

          本文标题:iOS 获取手机通讯录

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