iOS技术难点iOS开发技巧iOS

ios9.0 ContactsUI获取通讯录联系人信息

2016-07-10  本文已影响1427人  木头Lee

第1步:准备工作

第2步: 显示选择联系人控制器

//获取通讯录方法
- (void) getContactInfo
{
    //创建选择联系人的导航控制器
    peoplePickVC = [[CNContactPickerViewController alloc] init];
  
    //设置代理
    peoplePickVC.delegate = self;
    
    //弹出联系人界面
    [self showViewController:peoplePickVC sender:nil];
}

注意:如果Xcode提示:plugin com.apple.MobileAddressBook.ContactsViewService invalidated,只要把CNContactPickerViewController设置为全局变量即可:`

static CNContactPickerViewController * peoplePickVC;

第3步:实现代理方法

代理方法1:选中单个联系人时调用这个方法

getSingleContact.gif
-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact
{
    //获取联系人姓名
    NSString * firstName = contact.familyName;
    NSString * lastName = contact.givenName;
    NSLog(@"姓名:%@ %@",firstName,lastName);
    
    
    //数组保存各种类型的联系方式的字典(可以理解为字典) 字典的key和value分别对应号码类型和号码
    NSArray * phoneNums = contact.phoneNumbers;
    
    //通过遍历获取联系人各种类型的联系方式
    for (CNLabeledValue *labelValue in phoneNums)
    {
        //取出每一个字典,根据键值对取出号码和号码对应的类型
        NSString *phoneValue = [labelValue.value stringValue];
        NSString *phoneLabel = labelValue.label;
        NSLog(@"%@:%@",phoneLabel,phoneValue);
    }
}

代理方法2:选中联系人的单个号码类型时调用这个方法

getSingleContactProperty.gif
-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty
{
    //获取联系人信息
    NSString * firstName = contactProperty.contact.familyName;  //姓
    NSString * lastName = contactProperty.contact.givenName;    //名
    
    NSString * phoneNum = [contactProperty.value stringValue];  //号码
    NSString * phoneLabel = contactProperty.label;                //号码类型
    
    NSLog(@"姓名:%@ %@ \n %@:%@",firstName,lastName, phoneLabel, phoneNum);
}

代理方法3:选中多个联系人时调用这个方法

getMultiContact.gif
-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact *> *)contacts
{
    //遍历选中联系人数组,获取联系人姓名和电话
    for (CNContact * contact in contacts)
    {
        //获取联系人姓名
        NSString * firstName = contact.familyName;
        NSString * lastName = contact.givenName;
        NSLog(@"姓名:%@ %@",firstName,lastName);
        
        
        //数组保存各种类型的联系方式的字典(可以理解为字典) 字典的key和value分别对应号码类型和号码
        NSArray * phoneNums = contact.phoneNumbers;
        
        //通过遍历获取联系人各种类型的联系方式
        for (CNLabeledValue *labelValue in phoneNums)
        {
            //取出每一个字典,根据键值对取出号码和号码对应的类型
            NSString *phoneValue = [labelValue.value stringValue];
            NSString *phoneLabel = labelValue.label;
            NSLog(@"%@:%@",phoneLabel,phoneValue);
        }
    }
}

代理方法4:选中联系人的多个号码类型时调用这个方法

-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperties:(NSArray<CNContactProperty *> *)contactProperties
{
    //取出第一个属性,获取联系人姓名
    CNContactProperty * contactProperty = contactProperties.firstObject;
    NSString * firstName = contactProperty.contact.familyName;  //姓
    NSString * lastName = contactProperty.contact.givenName;    //名
    NSLog(@"姓名:%@ %@ ",firstName,lastName);
    
    //遍历选中多个类型号码数组的详细信息
    for (CNContactProperty * contactProperty in contactProperties)
    {
        NSString * phoneNum = [contactProperty.value stringValue];    //号码
        NSString * phoneLabel = contactProperty.label;                //号码类型
        
        NSLog(@"%@:%@",phoneLabel, phoneNum);
    }
}

注意:只实现该方法会停留在选择多个联系人界面,要对控制器的predicateForSelectionOfProperty属性设置要筛选的条件,只有满足筛选条件的联系人才可以被选中。这个怎么设置我实在编不下去了😜😜😜,好像是正则表达式的内容,也没找到相关资料,希望有大神看到不吝赐教!

peoplePickVC.predicateForSelectionOfProperty = [NSPredicate predicateWithFormat:@"(key == 'emailAddresses') AND (value LIKE '*@mac.com')"];

代理方法5:点击取消选择联系人(或联系人属性时调用这个方法)

cancelSelect.gif
-(void)contactPickerDidCancel:(CNContactPickerViewController *)picker
{
    NSLog(@"取消选择联系人!");
}
上一篇 下一篇

猜你喜欢

热点阅读