美文网首页
iOS获取通讯录信息

iOS获取通讯录信息

作者: 白石洲霍华德 | 来源:发表于2018-11-06 16:41 被阅读14次

    获取通讯录的信息有两种

    • 1.在不弹出通讯录控制器的情况下获取所有的通讯录信息
    • 2.通过弹窗获取通讯录的某个用户信息

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

    2.导入头文件

    #import <Contacts/Contacts.h>
    

    3.具体代码

    //请求通讯录权限
    #pragma mark 请求通讯录权限
    - (void)requestContactAuthorAfterSystemVersion9{
        
        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 {
                    NSLog(@"成功授权");
                }
            }];
        }
        else if(status == CNAuthorizationStatusRestricted)
        {
            NSLog(@"用户拒绝");
            [self showAlertViewAboutNotAuthorAccessContact];
        }
        else if (status == CNAuthorizationStatusDenied)
        {
            NSLog(@"用户拒绝");
            [self showAlertViewAboutNotAuthorAccessContact];
        }
        else if (status == CNAuthorizationStatusAuthorized)//已经授权
        {
            //有通讯录权限-- 进行下一步操作
            [self openContact];
        }
        
    }
    
    //有通讯录权限-- 进行下一步操作
    - (void)openContact{
        // 获取指定的字段,并不是要获取所有字段,需要指定具体的字段
        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;
            
         
            for (CNLabeledValue *labelValue in phoneNumbers) {
                //遍历一个人名下的多个电话号码
              
                CNPhoneNumber *phoneNumber = labelValue.value;
                
                NSString * string = phoneNumber.stringValue ;
                
                //去掉电话中的特殊字符
               
                
                NSLog(@"姓名=%@, 电话号码是=%@", nameStr, string);
                
                
            }
            
          
            
            
        }];
        
    }
    
    
    
    
    //提示没有通讯录权限
    - (void)showAlertViewAboutNotAuthorAccessContact{
        
        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];
    }
    
    

    第二种通过弹出通讯录控制器的情况下获取所有的通讯录信息

    1.导入头文件

    #import <Contacts/Contacts.h>
    #import <ContactsUI/ContactsUI.h>
    #import <AddressBook/AddressBook.h>
    #import <AddressBookUI/AddressBookUI.h>
    

    2.绑定代理

    CNContactPickerDelegate
    

    3.具体实现代码

    -(void)getPeopleClick
    {
        CNContactPickerViewController * contactPickerVc = [CNContactPickerViewController new];
        contactPickerVc.delegate = self;
        [[UIViewController currentViewController] presentViewController:contactPickerVc animated:YES completion:nil];
    }
    
    
    #pragma mark - CNContactPickerDelegate,
    - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
        
        
      
        NSString  *phone;
         NSString  *name;
        
     
      
        if (contact.phoneNumbers.count) {
            CNLabeledValue *firstLabeledValue = contact.phoneNumbers[0];
           ;
            
           phone=[[firstLabeledValue.value stringValue] stringByReplacingOccurrencesOfString:@"-" withString:@""];
        }
        
        name=[NSString stringWithFormat:@"%@%@",contact.familyName,contact.givenName];
        
        
        NSLog(@"====%@===%@",name,phone);
    
        
    }
    

    相关文章

      网友评论

          本文标题:iOS获取通讯录信息

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