标签:
内容均来自关东升老师的ios开发指南
上一篇写了联系人框架的一些必须知道的知识
如今写一下读取联系人数据相关操作
要读取通讯录数据库 须要
CFErrorRef error = NULL; //创建一个通讯录操作对象 ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
//查找联系人 - (void)filterContentForSearchText:(NSString *)searchText { //推断授权状态 if (ABAddressBookGetAuthorizationStatus()!=kABAuthorizationStatusAuthorized) { return ; } CFErrorRef error = NULL; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); if (searchText.length == 0) { //查找全部 listContacts = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook)); } else{ //依据字符串查找前缀keyword CFStringRef cfSearchText = (CFStringRef)CFBridgingRetain(searchText); listContacts = CFBridgingRelease(ABAddressBookCopyPeopleWithName(addressBook, cfSearchText)); CFRelease(cfSearchText); } //这里应该刷新表格 [self.tableView reloadData]; CFRelease(addressBook); }
授权的状态: kABAuthorizationStatusNotDetermined 用户还没有决定 kABAuthorizationStatusRestricted, 受限制 kABAuthorizationStatusDenied, 拒绝 kABAuthorizationStatusAuthorized 许可
联系人的属性
kABPersonFirstNameProperty; // 名字 kABPersonLastNameProperty; // 姓氏 kABPersonMiddleNameProperty; // 中间名 kABPersonPrefixProperty; // 前缀 kABPersonSuffixProperty; // 后缀 kABPersonNicknameProperty; // 昵称 kABPersonFirstNamePhoneticProperty; // 名字的汉语拼音或者音标 kABPersonLastNamePhoneticProperty; // 姓氏汉语拼音或者音标 kABPersonMiddleNamePhoneticProperty; // 中间名的汉语拼音或者音标 kABPersonOrganizationProperty; // 组织名 kABPersonJobTitleProperty; // 工作头衔 kABPersonDepartmentProperty; // 部门 kABPersonNoteProperty; // 备注 kABPersonBirthdayProperty; // 生日 kABPersonCreationDateProperty; // 创建时间 kABPersonModificationDateProperty; // 改动日期//多值属性 (一个属性中又多个值) 有标签、值、id kABPersonPhoneProperty ; //电话号码属性 kABMultiStringPropertyType;//类型 kABPersonEmailProperty ; //e-mail属性 kABMultiStringPropertyType;//类型 kABPersonURLProperty ; //URL属性 kABMultiStringPropertyType;//类型 kABPersonRelatedNamesProperty; // 亲属关系人属性 kABMultiStringPropertyType;//类型 kABPersonAddressProperty ; //地址属性 kABMultiDictionaryPropertyType;//类型 kABPersonInstantMessageProperty;//及时聊天属性 kABMultiDictionaryPropertyType;//类型 kABPersonSocialProfileProperty; //社交账号属性 kABMultiDictionaryPropertyTypel;//类型
利用上面的属性来查找想要的值
以下是单值属性:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } //从搜索出的联系人数组中获取一条数据 转换为ABRecordRef格式 ABRecordRef thisPerson = CFBridgingRetain([listContacts objectAtIndex:[indexPath row]]); //查找这条记录中的名字 NSString *firstName = CFBridgingRelease(ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty)); firstName = firstName != nil? firstName:@""; //查找这条记录中的姓氏 NSString *lastName = CFBridgingRelease(ABRecordCopyValue(thisPerson, kABPersonLastNameProperty)); cell.textLabel.text = [NSString stringWithFormat:@"%@%@",firstName,lastName]; CFRelease(thisPerson); return cell; }
//获取多值属性 - (void)multiValueProperty { ABRecordID personRecordID = [personIDASNumber intValue];//personIDASNumber是NSNumber类型的值用来保存RecordID的值 CFErrorRef error = NULL; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); //通过ABRecordID属性找到ABRecord ABRecordRef personRecord = ABAddressBookGetPersonWithRecordID(addressBook, personRecordID); //通过ABRecord 查找多值属性 ABMultiValueRef emailProperty = ABRecordCopyValue(personRecord, kABPersonEmailProperty); //将多值属性的多个值转化为数组 NSArray * emailArray = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(emailProperty)); for (int index = 0; index < emailArray.count; index++) { NSString *email = [emailArray objectAtIndex:index]; NSString *emailLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(emailProperty, index)); //推断当前这个值得标签 if ([emailLabel isEqualToString:(NSString *)kABWorkLabel]) { NSLog(@"%@", email); } } }
//获取联系人的图片 - (void)setImage { CFErrorRef error = NULL; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); ABRecordRef cecordRef = ABAddressBookGetPersonWithRecordID(addressBook,[personIDASNumber intValue]); //推断联系人是否有照片 if (ABPersonHasImageData(cecordRef)) { //获取照片数据 NSData *photoData = CFBridgingRelease(ABPersonCopyImageData(cecordRef)); self.imageView.image = [UIImage imageWithData:photoData]; } }
标签:
原文地址:http://www.cnblogs.com/bhlsheji/p/4350619.html