iOS开发调用系统通讯录获取电话号码

来源:互联网 发布:球球霸屏软件 编辑:程序博客网 时间:2024/06/13 01:21

iOS开发中常会遇到获取手机号码的需求,而获取号码一般有两种,一种是全部一次性取出,自己写控件对其进行展示,另一种是直接掉用系统通讯录,从中选取号码。本文所讲的就是后者。因为系统的原因,用ios7,ios8,ios9调用的方式都有区别

用到的库
AddressBook.framework,AddressBookUI.framework和Contacts.framework


头文件

#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: %@", (__bridgeNSError *)error);

                }

                else if (!granted)

                {

                    

                }

                else

                {

                    accessGranted = YES;

                }

        });

    }

    elseif (ABAddressBookGetAuthorizationStatus()==kABAuthorizationStatusAuthorized){

        accessGranted=YES;

    }

    else

    {

        NSLog(@"用户未授权提示");

        UIAlertView * alert = [[UIAlertViewalloc] initWithTitle:@"提示"message:@"您没有开启授权"delegate:self.vcrcancelButtonTitle:@"取消"otherButtonTitles:@"设置",nil];

        [alert show];

    }

    return accessGranted;

}


获取通讯录里的号码

if (IAIOS9) {

        CNContactPickerViewController * con = [[CNContactPickerViewControlleralloc]init];

        con.delegate = self;

        [self.vcrpresentViewController:con animated:YEScompletion:nil];

    }

    else{

        ABPeoplePickerNavigationController *nav = [[ABPeoplePickerNavigationControlleralloc] init];

        

        nav.peoplePickerDelegate =self;

        if(IAIOS8){

            nav.predicateForSelectionOfPerson = [NSPredicatepredicateWithValue:false];

        }

        [parentCtrl presentViewController:navanimated:animated completion:nil];

    }


协议的回调方法

//获取电话号码(ios9以上)

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty

{

    NSLog(@"%@",contactProperty.value);

    CNPhoneNumber * str = contactProperty.value;

    if ([str isKindOfClass:[CNPhoneNumberclass]]) {

        NSString * phone = [str.stringValuestringByReplacingOccurrencesOfString:@"-"withString:@""];

        phone = [phone stringByReplacingOccurrencesOfString:@"("withString:@""];

        phone = [phone stringByReplacingOccurrencesOfString:@")"withString:@""];

        phone = [phone stringByReplacingOccurrencesOfString:@" "withString:@""];

        NSLog(@"%@", phone);

        

        //返回电话号码

        self.editPhone(phone);

    }

    else{

        //返回电话号码

        self.editPhone(@"");

    }

    

    [picker dismissViewControllerAnimated:YEScompletion: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 = (__bridgeNSString *)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:YEScompletion: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 = (__bridgeNSString *)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:YEScompletion:nil];

        return NO;

    }

    return YES;

}


- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person

{

    ABPersonViewController *personViewController = [[ABPersonViewControlleralloc] init];

    personViewController.displayedPerson = person;

    [peoplePicker pushViewController:personViewControlleranimated:YES];

}


- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person

{

    return YES;

}


//取消选择(ios9以上)

- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker

{

    [picker dismissViewControllerAnimated:YEScompletion:nil];

}


//取消选择(<=ios8)

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker

{

    [peoplePicker dismissViewControllerAnimated:YEScompletion:nil];

}



1 0
原创粉丝点击