iOS 通讯录-获取联系人属性
2014-10-12 12:37
302 查看
内容均来自关东升老师的ios开发指南上一篇写了联系人框架的一些必须知道的知识现在写一下读取联系人数据相关操作要读取通讯录数据库 需要创建通讯录对象查询获取数据(所有或者部分)获取通讯录某一条记录(某个人的所有数据)获取这个人的各种属性数据就是这样
CFErrorRef error = NULL; //创建一个通讯录操作对象 ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);第一参数是保留参数,传递NULL即可;
//查找联系人 - (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{ //根据字符串查找前缀关键字 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; // 修改日期
//多值属性 (一个属性中又多个值) 有标签、值、idkABPersonPhoneProperty ; //电话号码属性 kABMultiStringPropertyType;//类型kABPersonEmailProperty ; //e-mail属性 kABMultiStringPropertyType;//类型kABPersonURLProperty ; //URL属性 kABMultiStringPropertyType;//类型kABPersonRelatedNamesProperty; // 亲属关系人属性 kABMultiStringPropertyType;//类型kABPersonAddressProperty ; //地址属性 kABMultiDictionaryPropertyType;//类型kABPersonInstantMessageProperty;//及时聊天属性 kABMultiDictionaryPropertyType;//类型kABPersonSocialProfileProperty; //社交账号属性 kABMultiDictionaryPropertyTypel;//类型利用上面的属性来查找想要的值[/code]
下面是单值属性:
- (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属性找到ABRecordABRecordRef 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];}}
相关文章推荐
- ios 获得通讯录中联系人的所有属性 亲测,可行 兼容io6 和 ios 7整理:iOS 短信与电话事件的获取
- iOS 通讯录-获取联系人属性
- iOS 获取通讯录中联系人的所有属性
- iOS 通讯录-获取联系人属性
- iOS 获取通讯录中联系人的所有属性
- 小胖说事39-----iOS 获取通讯录中联系人的所有属性
- iOS 获取通讯录中联系人的所有属性 by - zfqj
- iOS 获取通讯录中联系人的所有属性
- ios获得通讯录中联系人的所有属性
- iOS 获取通讯录联系人信息
- iOS开发之获取手机通讯录联系人信息<一>
- IOS获取系统通讯录联系人信息
- 通讯录联系人各属性获取
- iOS中获取本地通讯录联系人以及汉字首字母排序
- iOS 获取通信录联系人的所有属性(好东西必须留下)
- ios获得通讯录中联系人的所有属性
- iOS 获得通讯录中联系人的所有属性--b
- iOS获取通讯录联系人信息
- ios 获得通讯录中联系人的所有属性 亲测,可行 兼容io6 和 ios 7
- iOS中获取本地通讯录联系人以及汉字首字母排序