1、相关函数介绍
1、创建通讯录对象函数 ABAddressBookRef ABAddressBookCreateWithOptions( CFDictionaryRef options, CFErrorRef *error ); 例子: CFErrorRef error = NULL; ABAdressBookRef addressBook = ABAdressBookCreateWithOptions(NULL,&error); ABAddressBookRequestAccessWithCompletion(addressBook,^(bool granted,CFErrorRef error){ if(granted){ // 查询 ...... } }); CFRelease(addressBook); 2、查询联系人记录函数 CFArrayRef ABAddressBookCopyArrayOfAllPeople( ABAddressBookRef addressBook ); CFArrayRef ABAddressBookCopyPeopleWithName( ABAddressBookRef addressBook, CFStringRef name ); 例子: CFErrorRef error = NULL; ABAdressBookRef addressBook = ABAdressBookCreateWithOptions(NULL,&error); NSArray *listContacts = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook)); 3、读取单值属性函数 CFTypeRef ABRecordCopyValue( ABRecordRef record, ABPropertyID property ); 例子: ABRecordRef thisPerson = CFBridgingRetain(listContacts objectAtIndex:0); NSString *firstName = CFBridgingRelease(ABRecordCopyValue(thisPerson,kABPersonFirstNameProperty)); NSString *lastName = CFBridgingRelease(ABRecordCopyValue(thisPerson,kABPersonLastNameProperty)); CFRelease(thisPerson); 4、读取多值属性函数 同样使用ABRecordCopyValue,但返回值为ABMultiValueRef: ABMultiValueRef ABRecordCopyValue( ABRecordRef record, ABPropertyID property ); 例子: ABRecordRef thisPerson = ABAddressBookGetPersonWithRecordID(addressBook,personID); ABMultiValueRef emailsProperty = ABRecordCopyValue(thisPerson,kABPersonEmailProperty)); 5、从ABMultiValueRef对象返回CFArrayRef数组 CFArrayRef ABMultiValueCopyArrayOfAllValues( ABMultiValueRef multiValue ); 例子: NSArray *emailsArray = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(emailsProperty)); 6、从ABMultiValueRef对象返回CFStringRef标签 CFStringRef ABMultiValueCopyLabelAtIndex( ABMultiValueRef multiValue, CFIndex index ); 例子: NSString *emailLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(emailsProperty,index)); 7、读取图片属性函数 CFDataRef ABPersonCopyImageData( ABRecordRef person ); 例子: if(ABPersonHasImageData(person)){ NSData *photoData = CFBridgingRelease(ABPersonCopyImageData(person)); if(photoData){ ...... } }
2、案例代码
#import <UIKit/UIKit.h> #import <AddressBook/AddressBook.h> #import "T20140622175801DetailViewController.h" @interface T20140622175801ViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate> @property (nonatomic, strong) NSArray *listContacts; - (void)filterContentForSearchText:(NSString*)searchText; @end
#import "T20140622175801ViewController.h" @interface T20140622175801ViewController () @end @implementation T20140622175801ViewController - (void)viewDidLoad { [super viewDidLoad]; CFErrorRef error = NULL; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { if (granted) { //查询所有 [self filterContentForSearchText:@""]; } }); CFRelease(addressBook); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)filterContentForSearchText:(NSString*)searchText { //如果没有授权则退出 if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) { return ; } CFErrorRef error = NULL; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); if([searchText length]==0) { //查询所有 self.listContacts = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook)); } else { //条件查询 CFStringRef cfSearchText = (CFStringRef)CFBridgingRetain(searchText); self.listContacts = CFBridgingRelease(ABAddressBookCopyPeopleWithName(addressBook, cfSearchText)); CFRelease(cfSearchText); } [self.tableView reloadData]; CFRelease(addressBook); } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.listContacts count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } ABRecordRef thisPerson = CFBridgingRetain([self.listContacts objectAtIndex:[indexPath row]]); NSString *firstName = CFBridgingRelease(ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty)); firstName = firstName != nil?firstName:@""; NSString *lastName = CFBridgingRelease(ABRecordCopyValue(thisPerson, kABPersonLastNameProperty)); lastName = lastName != nil?lastName:@""; cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",firstName,lastName]; NSString* name = CFBridgingRelease(ABRecordCopyCompositeName(thisPerson)); cell.textLabel.text = name; CFRelease(thisPerson); return cell; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"showDetail"]) { NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; ABRecordRef thisPerson = CFBridgingRetain([self.listContacts objectAtIndex:[indexPath row]]); T20140622175801DetailViewController *detailViewController = [segue destinationViewController]; ABRecordID personID = ABRecordGetRecordID(thisPerson); NSNumber *personIDAsNumber = [NSNumber numberWithInt:personID]; detailViewController.personIDAsNumber = personIDAsNumber; CFRelease(thisPerson); } } #pragma mark --UISearchBarDelegate 协议方法 - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { //查询所有 [self filterContentForSearchText:@""]; } #pragma mark - UISearchDisplayController Delegate Methods //当文本内容发生改变时候,向表视图数据源发出重新加载消息 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { [self filterContentForSearchText:searchString]; //YES情况下表视图可以重新加载 return YES; } @end
#import <UIKit/UIKit.h> #import <AddressBook/AddressBook.h> @interface T20140622175801DetailViewController : UITableViewController @property (weak, nonatomic) IBOutlet UIImageView *imageView; @property (weak, nonatomic) IBOutlet UILabel *lblName; @property (weak, nonatomic) IBOutlet UILabel *lblMobile; @property (weak, nonatomic) IBOutlet UILabel *lblIPhone; @property (weak, nonatomic) IBOutlet UILabel *lblWorkEmail; @property (weak, nonatomic) IBOutlet UILabel *lblHomeEmail; @property (strong, nonatomic) NSNumber* personIDAsNumber; @end
#import "T20140622175801DetailViewController.h" @interface T20140622175801DetailViewController () @end @implementation T20140622175801DetailViewController - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; ABRecordID personID = [self.personIDAsNumber intValue]; CFErrorRef error = NULL; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, personID); //取得姓名属性 NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty)); firstName = firstName != nil?firstName:@""; NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty)); lastName = lastName != nil?lastName:@""; [self.lblName setText: [NSString stringWithFormat:@"%@ %@",firstName,lastName]]; NSString* name = CFBridgingRelease(ABRecordCopyCompositeName(person)); [self.lblName setText: name]; //取得Email属性 ABMultiValueRef emailsProperty = ABRecordCopyValue(person, kABPersonEmailProperty); NSArray* emailsArray = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(emailsProperty)); for(int index = 0; index< [emailsArray count]; index++){ NSString *email = [emailsArray objectAtIndex:index]; NSString *emailLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(emailsProperty, index)); if ([emailLabel isEqualToString:(NSString*)kABWorkLabel]) { [self.lblWorkEmail setText:email]; } else if ([emailLabel isEqualToString:(NSString*)kABHomeLabel]) { [self.lblHomeEmail setText:email]; } else { NSLog(@"%@: %@", @"其它Email", email); } } CFRelease(emailsProperty); //取得电话号码属性 ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(person, kABPersonPhoneProperty); NSArray* phoneNumberArray = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(phoneNumberProperty)); for(int index = 0; index< [phoneNumberArray count]; index++){ NSString *phoneNumber = [phoneNumberArray objectAtIndex:index]; NSString *phoneNumberLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phoneNumberProperty, index)); if ([phoneNumberLabel isEqualToString:(NSString*)kABPersonPhoneMobileLabel]) { [self.lblMobile setText:phoneNumber]; } else if ([phoneNumberLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) { [self.lblIPhone setText:phoneNumber]; } else { NSLog(@"%@: %@", @"其它电话", phoneNumber); } } CFRelease(phoneNumberProperty); //取得个人图片 if (ABPersonHasImageData(person)) { NSData *photoData = CFBridgingRelease(ABPersonCopyImageData(person)); if(photoData){ [self.imageView setImage:[UIImage imageWithData:photoData]]; } } CFRelease(addressBook); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
iOS.访问通讯录.01.读取联系人信息
时间: 2024-10-25 23:16:45