iOS 获取通讯录

来源:互联网 发布:sql server log工具 编辑:程序博客网 时间:2024/05/22 13:15

获取通讯录数据放到模型数组里

第一步

在调用改方法之前,需要先新建一个模型用来存放获取到的联系人数据,大概如下

@property(nonatomic,strong)NSString *firstName;
@property(nonatomic,strong)NSString *lastName;
@property(nonatomic,strong)NSString *middlename;
@property(nonatomic,strong)NSString *prefix;
@property(nonatomic,strong)NSString *suffix;
@property(nonatomic,strong)NSString *nickname;
@property(nonatomic,strong)NSString *firstnamePhonetic;
@property(nonatomic,strong)NSString *lastnamePhonetic;
@property(nonatomic,strong)NSString *middlenamePhonetic;
@property(nonatomic,strong)NSString *organization;
@property(nonatomic,strong)NSString *jobtitle;
@property(nonatomic,strong)NSString *department;
@property(nonatomic,strong)NSString *birthday;
@property(nonatomic,strong)NSString *note;
@property(nonatomic,strong)NSString *firstknow;
@property(nonatomic,strong)NSString *lastknow;
@property(nonatomic,strong)NSMutableArray *emailArr;
@property(nonatomic,strong)NSMutableArray *addressArr;
@property(nonatomic,strong)NSMutableArray *phoneArr;
@property(nonatomic,strong)NSMutableArray *urlArr;

第二步

直接调用该方法,下面代码里connectPeopleArr是个数组,用来接收获取到的存放着联系人数据的模型,MZConnectPeopleModel即为上面内容的联系人模型

- (void)loadPerson

{
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
    
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error){
            
            CFErrorRef *error1 = NULL;
            ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error1);
            [self copyAddressBook:addressBook];
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){
        
        CFErrorRef *error = NULL;
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
        [self copyAddressBook:addressBook];
    }
    else {
        dispatch_async(dispatch_get_main_queue(), ^{
            // 更新界面
            [SVProgressHUD showInfoWithStatus:@"没有通讯录权限,请在设置里允许访问通讯录"];
        });
    }
}
- (void)copyAddressBook:(ABAddressBookRef)addressBook
{
    CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
    
    for ( int i = 0; i < numberOfPeople; i++){
        MZConnectPeopleModel *peopleModel = [[MZConnectPeopleModel alloc]init];
        
        ABRecordRef person = CFArrayGetValueAtIndex(people, i);
        
        peopleModel.firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
        peopleModel.lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
        
        //读取middlename
        peopleModel.middlename = [NSString stringWithFormat:@"%@",ABRecordCopyValue(person, kABPersonMiddleNameProperty)];
        //读取prefix前缀
        peopleModel.prefix = [NSString stringWithFormat:@"%@",ABRecordCopyValue(person, kABPersonPrefixProperty)];
        //读取suffix后缀
        peopleModel.suffix = [NSString stringWithFormat:@"%@",ABRecordCopyValue(person, kABPersonSuffixProperty)];
        //读取nickname呢称
        peopleModel.nickname = [NSString stringWithFormat:@"%@",ABRecordCopyValue(person, kABPersonNicknameProperty)];
        //读取firstname拼音音标
        peopleModel.firstnamePhonetic = [NSString stringWithFormat:@"%@",ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty)];
        //读取lastname拼音音标
        peopleModel.lastnamePhonetic = [NSString stringWithFormat:@"%@",ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty)];
        //读取middlename拼音音标
        peopleModel.middlenamePhonetic = [NSString stringWithFormat:@"%@",ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty)];
        //读取organization公司
        peopleModel.organization = [NSString stringWithFormat:@"%@",ABRecordCopyValue(person, kABPersonOrganizationProperty)];
        //读取jobtitle工作
        peopleModel.jobtitle = [NSString stringWithFormat:@"%@",ABRecordCopyValue(person, kABPersonJobTitleProperty)];
        //读取department部门
        peopleModel.department = [NSString stringWithFormat:@"%@",ABRecordCopyValue(person, kABPersonDepartmentProperty)];
        //读取birthday生日
        peopleModel.birthday = [NSString stringWithFormat:@"%@",ABRecordCopyValue(person, kABPersonBirthdayProperty)];
        //读取note备忘录
        peopleModel.note = [NSString stringWithFormat:@"%@",ABRecordCopyValue(person, kABPersonNoteProperty)];
        //第一次添加该条记录的时间
        peopleModel.firstknow = [NSString stringWithFormat:@"%@",ABRecordCopyValue(person, kABPersonCreationDateProperty)];
        //最后一次修改該条记录的时间
        peopleModel.lastknow = [NSString stringWithFormat:@"%@",ABRecordCopyValue(person, kABPersonModificationDateProperty)];
        
        //获取email多值
        ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);
        int emailcount = ABMultiValueGetCount(email);
        for (int x = 0; x < emailcount; x++)
        {
            //获取email Label
            NSString* emailLabel =[NSString stringWithFormat:@"%@", ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(email, x))];
            //获取email值
            NSString* emailContent = [NSString stringWithFormat:@"%@",ABMultiValueCopyValueAtIndex(email, x)];
            peopleModel.emailArr = [NSMutableArray arrayWithObjects:emailLabel,emailContent, nil];
        }
        //读取地址多值
        ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);
        int count = ABMultiValueGetCount(address);
        
        for(int j = 0; j < count; j++)
        {
            //获取地址Label
            NSString* addressLabel = [NSString stringWithFormat:@"%@",ABMultiValueCopyLabelAtIndex(address, j)];
            //获取該label下的地址6属性
            NSDictionary* personaddress = (__bridge NSDictionary *)ABMultiValueCopyValueAtIndex(address, j);
            NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];
            NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];
            NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];
            NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];
            NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey];
            NSString* coutntrycode = [personaddress valueForKey:(NSString *)kABPersonAddressCountryCodeKey];
        }
/*
        //获取dates多值
        ABMultiValueRef dates = ABRecordCopyValue(person, kABPersonDateProperty);
        int datescount = ABMultiValueGetCount(dates);
        for (int y = 0; y < datescount; y++)
        {
            //获取dates Label
            NSString* datesLabel = [NSString stringWithFormat:@"%@",ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(dates, y))];
            //获取dates值
            NSString* datesContent = [NSString stringWithFormat:@"%@",ABMultiValueCopyValueAtIndex(dates, y)];
        }
        //获取kind值
        CFNumberRef recordType = ABRecordCopyValue(person, kABPersonKindProperty);
        if (recordType == kABPersonKindOrganization) {
            // it's a company
            NSLog(@"it's a company\n");
        } else {
            // it's a person, resource, or room
            NSLog(@"it's a person, resource, or room\n");
        }
        
        
        //获取IM多值
        ABMultiValueRef instantMessage = ABRecordCopyValue(person, kABPersonInstantMessageProperty);
        for (int l = 1; l < ABMultiValueGetCount(instantMessage); l++)
        {
            //获取IM Label
            NSString* instantMessageLabel = (NSString*)ABMultiValueCopyLabelAtIndex(instantMessage, l);
            //获取該label下的2属性
            NSDictionary* instantMessageContent =(NSDictionary*) ABMultiValueCopyValueAtIndex(instantMessage, l);
            NSString* username = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageUsernameKey];
            
            NSString* service = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageServiceKey];
        }
 */
        //读取电话多值
        ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
        for (int k = 0; k<ABMultiValueGetCount(phone); k++)
        {
            //获取电话Label
            NSString * personPhoneLabel = [NSString stringWithFormat:@"%@",ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k))];
            //获取該Label下的电话值
            NSString * personPhone = [NSString stringWithFormat:@"%@",ABMultiValueCopyValueAtIndex(phone, k)];
            peopleModel.phoneArr = [NSMutableArray arrayWithObjects:personPhoneLabel,personPhone, nil];
        }
 /*
        //获取URL多值
        ABMultiValueRef url = ABRecordCopyValue(person, kABPersonURLProperty);
        for (int m = 0; m < ABMultiValueGetCount(url); m++)
        {
            //获取电话Label
            NSString * urlLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(url, m));
            //获取該Label下的电话值
            NSString * urlContent = (NSString*)ABMultiValueCopyValueAtIndex(url,m);
        }
        
        //读取照片
        NSData *image = (NSData*)ABPersonCopyImageData(person);
*/
        [_connectPeopleArr addObject:peopleModel];
        NSLog(@"%@",peopleModel.firstName);//名字
        NSLog(@"%@",peopleModel.firstknow);
        NSLog(@"%@",peopleModel.firstnamePhonetic);
        NSLog(@"%@",peopleModel.middlename);
        NSLog(@"%@",peopleModel.prefix);
        NSLog(@"%@",peopleModel.suffix);
        NSLog(@"%@",peopleModel.note);
        NSLog(@"%@",peopleModel.lastknow);
        NSLog(@"%@",peopleModel.nickname);
        NSLog(@"%@",peopleModel.organization);//公司
        NSLog(@"%@",peopleModel.birthday);
        NSLog(@"%@",peopleModel.department);
        NSLog(@"%@",peopleModel.middlenamePhonetic);
        NSLog(@"%@",peopleModel.lastName);//姓氏
        NSLog(@"%@",peopleModel.phoneArr[1]);//"住宅:11123456666"

        }

}
0 0