因为ios系统对用户隐私的控制,第三方应用程序只能经过苹果官方接口调用系统通信录,不能像android那样直接操做通信录数据库。
通常地,使用系统自带通信录的方法有两种,一种是直接将整个通信录引入到应用程序,另外一种是逐条读取通信录中的每一条联系人信息。下面咱们就一一详解。android
1 直接引用整个通信录ios
使用的类:ABPeoplePickerNavigationController
方法:数据库
在LocalAddressBookController.h文件中 <UIKit/UIKit.h> <AddressBook/AddressBook.h> <AddressBookUI/AddressBookUI.h> LocalAddressBookController : UIViewController<ABPersonViewControllerDelegate,ABPeoplePickerNavigationControllerDelegate,ABNewPersonViewControllerDelegate> { ABPeoplePickerNavigationController *picker; ABNewPersonViewController *personViewController; }在LocalAddressBookController.m文件中 LocalAddressBookController - ()initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; (self) { } self; } - ()didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } mark - View lifecycle - ()viewDidLoad { [super viewDidLoad]; picker = [[ABPeoplePickerNavigationController alloc]init]; picker.view.frame = CGRectMake(, , Screen_width, Screen_height-); picker.peoplePickerDelegate = self; picker. = self; [picker setHidesBottomBarWhenPushed:YES]; [picker setNavigationBarHidden:NO animated:NO];//显示上方的NavigationBar和搜索框 [self.view addSubview:picker.view]; } mark UINavigationControllerDelegate methods- ()navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { UIView *custom = [[UIView alloc] initWithFrame:CGRectMake(,,,)]; UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithCustomView:custom]; [viewController.navigationItem setRightBarButtonItem:btn animated:NO]; [btn release]; [custom release]; } mark - peoplePickerDelegate Methods -(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { NSLog(, (NSString*)ABRecordCopyCompositeName(person)); YES; } -(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { YES; } -(BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{ YES; } -()peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker { [picker dismissModalViewControllerAnimated:YES]; } //……
ios6 运行效果:app
ios7 运行效果:ide
经验:url
当咱们将系统通信录整个引入的时候,在通信录的右上角有一个系统自带的“取消”按钮。如何才能将这个取消按钮隐藏呢?spa
(1)方法1:若是你的应用程序是用企业证书开发,不须要提交到appStore进行审核,那么答案很是简单,为响应的piker增长以下代码便可:code
[picker setAllowsCancel:NO];orm
(2)方法二:上面的方法是非公开方法,是没法经过appStore审核的。若是想经过审核。能够尝试使用以下方法:接口
前提:设置 picker.delegate = self; 而后实现以下委托方法 - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { UIView *custom = [[UIView alloc] initWithFrame:CGRectMake(0.0f,0.0f,0.0f,0.0f)]; UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithCustomView:custom]; //UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction)]; [viewController.navigationItem setRightBarButtonItem:btn animated:NO]; [btn release]; [custom release]; }
或者:(比上面更好)
前提:设置 picker.delegate = self; 而后实现以下委托方法,下面实现的效果,要比上面的好。上面实现的效果,当点击“搜索”框时,“取消”按钮还会从新出现。 - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { // Here we want to remove the 'Cancel' button, but only if we're showing // either of the ABPeoplePickerNavigationController's top two controllers if ([navigationController.viewControllers indexOfObject:viewController] <= 1) { viewController.navigationItem.rightBarButtonItem = nil; } }
二、逐条读取通信录中的每一条联系人信息。
方法:在上述类中,直接添加以下方法便可
在LocalAddressBookController.h文件中 <UIKit/UIKit.h> <AddressBook/AddressBook.h> <AddressBookUI/AddressBookUI.h> LocalAddressBookController : { UITextView *textView; }在LocalAddressBookController.m文件中 LocalAddressBookController - ()initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; (self) { } self; } - ()didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } mark - View lifecycle - ()viewDidLoad { [super viewDidLoad]; textView = [[UITextView alloc]initWithFrame:CGRectMake(, , Screen_width, Screen_height)]; [self getAddressBook]; [self.view addSubview:textView]; }-()getAddressBook { ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook); ( i = ; i < CFArrayGetCount(results); i++) { ABRecordRef person = CFArrayGetValueAtIndex(results, i); NSString *personName = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty); (personName != nil) textView.text = [textView.text stringByAppendingFormat:,personName]; NSString *lastname = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty); (lastname != nil) textView.text = [textView.text stringByAppendingFormat:,lastname]; NSString *middlename = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty); (middlename != nil) textView.text = [textView.text stringByAppendingFormat:,middlename]; NSString *prefix = (NSString*)ABRecordCopyValue(person, kABPersonPrefixProperty); (prefix != nil) textView.text = [textView.text stringByAppendingFormat:,prefix]; NSString *suffix = (NSString*)ABRecordCopyValue(person, kABPersonSuffixProperty); (suffix != nil) textView.text = [textView.text stringByAppendingFormat:,suffix]; NSString *nickname = (NSString*)ABRecordCopyValue(person, kABPersonNicknameProperty); (nickname != nil) textView.text = [textView.text stringByAppendingFormat:,nickname]; NSString *firstnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty); (firstnamePhonetic != nil) textView.text = [textView.text stringByAppendingFormat:,firstnamePhonetic]; NSString *lastnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty); (lastnamePhonetic != nil) textView.text = [textView.text stringByAppendingFormat:,lastnamePhonetic]; NSString *middlenamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty); (middlenamePhonetic != nil) textView.text = [textView.text stringByAppendingFormat:,middlenamePhonetic]; NSString *organization = (NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty); (organization != nil) textView.text = [textView.text stringByAppendingFormat:,organization]; NSString *jobtitle = (NSString*)ABRecordCopyValue(person, kABPersonJobTitleProperty); (jobtitle != nil) textView.text = [textView.text stringByAppendingFormat:,jobtitle]; NSString *department = (NSString*)ABRecordCopyValue(person, kABPersonDepartmentProperty); (department != nil) textView.text = [textView.text stringByAppendingFormat:,department]; NSDate *birthday = (NSDate*)ABRecordCopyValue(person, kABPersonBirthdayProperty); (birthday != nil) textView.text = [textView.text stringByAppendingFormat:,birthday]; NSString *note = (NSString*)ABRecordCopyValue(person, kABPersonNoteProperty); (note != nil) textView.text = [textView.text stringByAppendingFormat:,note]; NSString *firstknow = (NSString*)ABRecordCopyValue(person, kABPersonCreationDateProperty); NSLog(,firstknow); NSString *lastknow = (NSString*)ABRecordCopyValue(person, kABPersonModificationDateProperty); NSLog(,lastknow); ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty); emailcount = ABMultiValueGetCount(email); ( x = ; x < emailcount; x++) { NSString* emailLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(email, x)); NSString* emailContent = (NSString*)ABMultiValueCopyValueAtIndex(email, x); textView.text = [textView.text stringByAppendingFormat:,emailLabel,emailContent]; } ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty); count = ABMultiValueGetCount(address); ( j = ; j < count; j++) { NSString* addressLabel = (NSString*)ABMultiValueCopyLabelAtIndex(address, j); textView.text = [textView.text stringByAppendingFormat:,addressLabel]; NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(address, j); NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey]; (country != nil) textView.text = [textView.text stringByAppendingFormat:,country]; NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey]; (city != nil) textView.text = [textView.text stringByAppendingFormat:,city]; NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey]; (state != nil) textView.text = [textView.text stringByAppendingFormat:,state]; NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey]; (street != nil) textView.text = [textView.text stringByAppendingFormat:,street]; NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey]; (zip != nil) textView.text = [textView.text stringByAppendingFormat:,zip]; NSString* coutntrycode = [personaddress valueForKey:(NSString *)kABPersonAddressCountryCodeKey]; (coutntrycode != nil) textView.text = [textView.text stringByAppendingFormat:,coutntrycode]; } ABMultiValueRef dates = ABRecordCopyValue(person, kABPersonDateProperty); datescount = ABMultiValueGetCount(dates); ( y = ; y < datescount; y++) { NSString* datesLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(dates, y)); NSString* datesContent = (NSString*)ABMultiValueCopyValueAtIndex(dates, y); textView.text = [textView.text stringByAppendingFormat:,datesLabel,datesContent]; } CFNumberRef recordType = ABRecordCopyValue(person, kABPersonKindProperty); (recordType == kABPersonKindOrganization) { NSLog(); } { NSLog(); } ABMultiValueRef instantMessage = ABRecordCopyValue(person, kABPersonInstantMessageProperty); ( l = ; l < ABMultiValueGetCount(instantMessage); l++) { NSString* instantMessageLabel = (NSString*)ABMultiValueCopyLabelAtIndex(instantMessage, l); textView.text = [textView.text stringByAppendingFormat:,instantMessageLabel]; NSDictionary* instantMessageContent =(NSDictionary*) ABMultiValueCopyValueAtIndex(instantMessage, l); NSString* username = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageUsernameKey]; (username != nil) textView.text = [textView.text stringByAppendingFormat:,username]; NSString* service = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageServiceKey]; (service != nil) textView.text = [textView.text stringByAppendingFormat:,service]; } ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty); ( k = ; k<ABMultiValueGetCount(phone); k++) { NSString * personPhoneLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k)); NSString * personPhone = (NSString*)ABMultiValueCopyValueAtIndex(phone, k); textView.text = [textView.text stringByAppendingFormat:,personPhoneLabel,personPhone]; } ABMultiValueRef url = ABRecordCopyValue(person, kABPersonURLProperty); ( m = ; m < ABMultiValueGetCount(url); m++) { NSString * urlLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(url, m)); NSString * urlContent = (NSString*)ABMultiValueCopyValueAtIndex(url,m); textView.text = [textView.text stringByAppendingFormat:,urlLabel,urlContent]; } NSData *image = (NSData*)ABPersonCopyImageData(person); UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )]; [myImage setImage:[UIImage imageWithData:image]]; myImage.opaque = YES; [textView addSubview:myImage]; } CFRelease(results); CFRelease(addressBook); } //…… @end
ios7运行效果: