標籤:ios 通訊錄
內容均來自關東升老師的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; // 修改日期
//多值屬性 (一個屬性中又多個值) 有標籤、值、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]; }}
iOS 通訊錄-擷取連絡人屬性