iOS开发中常会遇到获取手机号码的需求,而获取号码一般有两种,一种是全部一次性取出,自己写控件对其进行展示,另一种是直接掉用系统通讯录,从中选取号码。本文所讲的就是后者。因为系统的原因,用ios7,ios8,ios9调用的方式都有区别
#import <AddressBookUI/ABPeoplePickerNavigationController.h>
#import <AddressBook/ABPerson.h>
#import <AddressBookUI/ABPersonViewController.h>
#import <Contacts/Contacts.h>
#import <Contacts/ContactsDefines.h>
#import <ContactsUI/CNContactPickerViewController.h>
#import <ContactsUI/CNContactViewController.h>
#import <ContactsUI/ContactsUI.h>
协议
ABPeoplePickerNavigationControllerDelegate,CNContactPickerDelegate,CNContactViewControllerDelegate
//通讯录授权
- (BOOL)accessTheAddress
{
CFErrorRef *error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
__block BOOL accessGranted = NO;
if (ABAddressBookGetAuthorizationStatus()==kABAuthorizationStatusNotDetermined){
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted=granted;
if (error){
NSLog(@"Error: %@", (__bridge NSError *)error);
}
else if (!granted)
{
}
else
{
accessGranted = YES;
}
});
}
else if (ABAddressBookGetAuthorizationStatus()==kABAuthorizationStatusAuthorized){
accessGranted=YES;
}
else
{
NSLog(@"用户未授权提示");
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您没有开启授权" delegate:self.vcr cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
[alert show];
}
return accessGranted;
}
获取通讯录里的号码
if (IAIOS9) {
CNContactPickerViewController * con = [[CNContactPickerViewController alloc]init];
con.delegate = self;
[self.vcr presentViewController:con animated:YES completion:nil];
}
else{
ABPeoplePickerNavigationController *nav = [[ABPeoplePickerNavigationController alloc] init];
nav.peoplePickerDelegate = self;
if(IAIOS8){
nav.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];
}
[parentCtrl presentViewController:nav animated:animated completion:nil];
}
协议的回调方法
//获取电话号码(ios9以上)
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty
{
NSLog(@"%@",contactProperty.value);
CNPhoneNumber * str = contactProperty.value;
if ([str isKindOfClass:[CNPhoneNumber class]]) {
NSString * phone = [str.stringValue stringByReplacingOccurrencesOfString:@"-" withString:@""];
phone = [phone stringByReplacingOccurrencesOfString:@"(" withString:@""];
phone = [phone stringByReplacingOccurrencesOfString:@")" withString:@""];
phone = [phone stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"%@", phone);
//返回电话号码
self.editPhone(phone);
}
else{
//返回电话号码
self.editPhone(@"");
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
//获取电话号码(ios8)
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
ABMultiValueRef phoneNumber = ABRecordCopyValue(person, kABPersonPhoneProperty);
long index = ABMultiValueGetIndexForIdentifier(phoneNumber,identifier);
NSString *phone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneNumber, index);
if ([phone hasPrefix:@"+"]) {
phone = [phone substringFromIndex:3];
}
phone = [phone stringByReplacingOccurrencesOfString:@"-" withString:@""];
phone = [phone stringByReplacingOccurrencesOfString:@"(" withString:@""];
phone = [phone stringByReplacingOccurrencesOfString:@")" withString:@""];
phone = [phone stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"%@", phone);
//返回电话号码
self.editPhone(phone);
if (phoneNumber) {
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
return;
}
}
//获取电话号码(ios7)
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
ABMultiValueRef phoneNumber = ABRecordCopyValue(person, kABPersonPhoneProperty);
long index = ABMultiValueGetIndexForIdentifier(phoneNumber,identifier);
NSString *phone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneNumber, index);
if ([phone hasPrefix:@"+"]) {
phone = [phone substringFromIndex:3];
}
phone = [phone stringByReplacingOccurrencesOfString:@"-" withString:@""];
phone = [phone stringByReplacingOccurrencesOfString:@"(" withString:@""];
phone = [phone stringByReplacingOccurrencesOfString:@")" withString:@""];
phone = [phone stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"%@", phone);
//返回电话号码
self.editPhone(phone);
if (phoneNumber) {//&& [ZXValidateHelper checkTel:phoneNO]
// phoneNum = phoneNO;
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
return NO;
}
return YES;
}
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person
{
ABPersonViewController *personViewController = [[ABPersonViewController alloc] init];
personViewController.displayedPerson = person;
[peoplePicker pushViewController:personViewController animated:YES];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
return YES;
}
//取消选择(ios9以上)
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
//取消选择(<=ios8)
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
}