获取联系人信息

//这个变量用于记录授权是否成功,即用户是否允许我们访问通讯录
    int __block tip=0;
    //声明一个通讯簿的引用
    ABAddressBookRef addBook =nil;
    //因为在IOS6.0之后和之前的权限申请方式有所差别,这里做个判断
    if ([[UIDevice currentDevice].systemVersion floatValue]>=6.0) {
        //创建通讯簿的引用
        addBook=ABAddressBookCreateWithOptions(NULL, NULL);
        //创建一个出事信号量为0的信号
        dispatch_semaphore_t sema=dispatch_semaphore_create(0);
        //申请访问权限
        ABAddressBookRequestAccessWithCompletion(addBook, ^(bool greanted, CFErrorRef error)        {
            //greanted为YES是表示用户允许,否则为不允许
            if (!greanted) {
                tip=1;
            }
            //发送一次信号
            dispatch_semaphore_signal(sema);
        });
        //等待信号触发
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    }else{
        //IOS6之前
        addBook =ABAddressBookCreate();
    }
    if (tip) {
        //做一个友好的提示
        UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您设置允许APP访问您的通讯录\nSettings>General>Privacy" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alart show];
        return;
    }
   
   
   
    //获取所有联系人的数组
    CFArrayRef allLinkPeople = ABAddressBookCopyArrayOfAllPeople(addBook);
    //获取联系人总数
    CFIndex number = ABAddressBookGetPersonCount(addBook);
    //进行遍历
    for (NSInteger i=0; i<number; i++) {
        //获取联系人对象的引用
        ABRecordRef  people = CFArrayGetValueAtIndex(allLinkPeople, i);
        //获取当前联系人名字
      //  NSString*firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));
        //获取当前联系人姓氏
      //  NSString*lastName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty));
        //获取当前联系人中间名
      //  NSString*middleName=( NSString*)CFBridgingRelease(ABRecordCopyValue(people, kABPersonMiddleNameProperty));
        //获取当前联系人的名字前缀
      //  NSString*prefix=( NSString*)CFBridgingRelease(ABRecordCopyValue(people, kABPersonPrefixProperty));
        //获取当前联系人的名字后缀
       // NSString*suffix=( NSString*)CFBridgingRelease(ABRecordCopyValue(people, kABPersonSuffixProperty));
        //获取当前联系人的昵称
      //  NSString*nickName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonNicknameProperty));
        //获取当前联系人的名字拼音
       // NSString*firstNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonFirstNamePhoneticProperty));
        //获取当前联系人的姓氏拼音
        //NSString*lastNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonLastNamePhoneticProperty));
        //获取当前联系人的中间名拼音
      //  NSString*middleNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNamePhoneticProperty));
        //获取当前联系人的公司
       // NSString*organization=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonOrganizationProperty));
        //获取当前联系人的职位
       // NSString*job=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonJobTitleProperty));
        //获取当前联系人的部门
      //  NSString*department=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonDepartmentProperty));
        //获取当前联系人的生日
      //  NSString*birthday=( NSDate*)CFBridgingRelease(ABRecordCopyValue(people, kABPersonBirthdayProperty));
        NSMutableArray * emailArr = [[NSMutableArray alloc]init];
        //获取当前联系人的邮箱 注意是数组
        ABMultiValueRef emails= ABRecordCopyValue(people, kABPersonEmailProperty);
        for (NSInteger j=0; j<ABMultiValueGetCount(emails); j++) {
            [emailArr addObject:(__bridge NSString *)(ABMultiValueCopyValueAtIndex(emails, j))];
        }
        //获取当前联系人的备注
        //NSString*notes=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonNoteProperty));
        //获取当前联系人的电话 数组
      //  NSMutableArray * phoneArr = [[NSMutableArray alloc]init];
        ABMultiValueRef phones= ABRecordCopyValue(people, kABPersonPhoneProperty);
        for (NSInteger j=0; j<ABMultiValueGetCount(phones); j++) {
            [self.phoneArray addObject:(__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j))];
          
        }
       //  NSLog(@"%@",phoneArr);
        //[self.phoneArray addObject:phoneArr];
        //获取创建当前联系人的时间 注意是NSDate
    //    NSDate*creatTime=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonCreationDateProperty));
        //获取最近修改当前联系人的时间
    //    NSDate*alterTime=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonModificationDateProperty));
        //获取地址
       // ABMultiValueRef address = ABRecordCopyValue(people, kABPersonAddressProperty);
//        for (int j=0; j<ABMultiValueGetCount(address); j++) {
//            //地址类型
//            NSString * type = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(address, j));
//            NSDictionary * temDic = (__bridge NSDictionary *)(ABMultiValueCopyValueAtIndex(address, j));
//            //地址字符串,可以按需求格式化
//            NSString * adress = [NSString stringWithFormat:@"国家:%@\n省:%@\n市:%@\n街道:%@\n邮编:%@",[temDic valueForKey:(NSString*)kABPersonAddressCountryKey],[temDic valueForKey:(NSString*)kABPersonAddressStateKey],[temDic valueForKey:(NSString*)kABPersonAddressCityKey],[temDic valueForKey:(NSString*)kABPersonAddressStreetKey],[temDic valueForKey:(NSString*)kABPersonAddressZIPKey]];
//        }
        //获取当前联系人头像图片
//        NSData*userImage=(__bridge NSData*)(ABPersonCopyImageData(people));
//        //获取当前联系人纪念日
//        NSMutableArray * dateArr = [[NSMutableArray alloc]init];
//        ABMultiValueRef dates= ABRecordCopyValue(people, kABPersonDateProperty);
//        for (NSInteger j=0; j<ABMultiValueGetCount(dates); j++) {
//            //获取纪念日日期
//            NSDate * data =(__bridge NSDate*)(ABMultiValueCopyValueAtIndex(dates, j));
//            //获取纪念日名称
//            NSString * str =(__bridge NSString*)(ABMultiValueCopyLabelAtIndex(dates, j));
//            NSDictionary * temDic = [NSDictionary dictionaryWithObject:data forKey:str];
//            [dateArr addObject:temDic];
//        }
    }

posted @ 2016-04-13 23:32  huangyan1022  阅读(197)  评论(0编辑  收藏  举报