昨天因项目需求要访问系统通信录获取电话号码,因而乎从一无所知,开始倒腾,倒腾了一下午,总算了弄好了。写这边博客是为了记录一下,本身下一次弄的时候就别在出错了。同时,有和我同样的菜鸟可以避免走一下弯路。框架
好了,言归正传,要访问系统的通信录,首先须要添加AddressBook.framework
和AddressBookUI.framework
两个框架到你工程中build phase的"Link Binary With Libraries"之下,而后就能够开始了。ide
首先咱们须要建立一个控制器:ViewController,在.h文件中导入头文件:<AddressBook/AddressBook.h>、 <AddressBookUI/AddressBookUI.h>,ui
#import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h>
而后在控制器实现ABPeoplePickerNavigationControllerDelegate, UINavigationControllerDelegate协议spa
@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate, UINavigationControllerDelegate>
在viewDidAppear方法中建立ABPeoplePickerNavigationController,同时设置viewController做为委托对象code
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; ABPeoplePickerNavigationController *pNC = [[ABPeoplePickerNavigationController alloc] init]; pNC.peoplePickerDelegate = self; [self presentViewController:pNC animated:YES completion:nil]; }
接下来须要实现ABPeoplePickerNavigationControllerDelegate协议对象
#pragma mark - ABPeoplePickerNavigationControllerDelegate - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty); long index = ABMultiValueGetIndexForIdentifier(phone,identifier); NSString *phoneNO = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phone, index); phoneNO = [phoneNO stringByReplacingOccurrencesOfString:@"-" withString:@""]; NSLog(@"%@", phoneNO); if (phone && phoneNO.length == 11) { [peoplePicker dismissViewControllerAnimated:YES completion:nil]; return; }else{ UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"错误提示" message:@"请选择正确手机号" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil]; [alertView show]; } } - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person NS_AVAILABLE_IOS(8_0) { ABPersonViewController *personViewController = [[ABPersonViewController alloc] init]; personViewController.displayedPerson = person; [peoplePicker pushViewController:personViewController animated:YES]; } - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker { [peoplePicker dismissViewControllerAnimated:YES completion:nil]; } - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person NS_DEPRECATED_IOS(2_0, 8_0) { return YES; } - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_DEPRECATED_IOS(2_0, 8_0) { ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty); long index = ABMultiValueGetIndexForIdentifier(phone,identifier); NSString *phoneNO = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phone, index); phoneNO = [phoneNO stringByReplacingOccurrencesOfString:@"-" withString:@""]; NSLog(@"%@", phoneNO); if (phone && phoneNO.length == 11) { [peoplePicker dismissViewControllerAnimated:YES completion:nil]; return NO; }else{ UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"错误提示" message:@"请选择正确手机号" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil]; [alertView show]; } return YES; }@end
到这里本觉得大功告成,的确在iOS7是没有任何问题,可是iOS8出现了坑爹的问题,就是选择联系人后blog
ABPeoplePickerNavigationController会自动dismiss掉,这个问题可坑坏我了。问了谷歌和度娘,在stackvoerflow找到了相似的问题,可是都没有获得解决,在以为没有办法的时候,又开始看ABPeoplePickerNavigationController.h的头文件,发现了ci
predicateForSelectionOfPerson属性,因而乎在viewDidAppear方法中加入以下代码:博客
if([[UIDevice currentDevice].systemVersion floatValue] >= 8.0){ pNC.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false]; }
运行程序,大功告成。string