ios打开通讯录及点击通讯录时提取相关信息

 首先引入头文件

#import <AddressBookUI/ABPeoplePickerNavigationController.h>

#import <AddressBook/ABPerson.h>

#import <AddressBookUI/ABPersonViewController.h>

在需要打开电话本的方法中实现:

    

- (IBAction)openPhoneBook:(UIButton *)sender {

    NSLog(@"打开电话本");

    ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];

    peoplePicker.peoplePickerDelegate = self;

    peoplePicker.delegate = self;

    [self presentViewController:peoplePicker animated:YES completion:nil];

}

代理方法实现回调

#pragma mark - ABPeoplePickerNavigationControllerDelegate

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker

{

    [peoplePicker dismissViewControllerAnimated:YES completion:^{}];

}

 

 

// Called after a person has been selected by the user.

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

{

    ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);

    NSString * phone  = @"";

    if (phoneNumbers) {

        //CFIndex count = ABMultiValueGetCount(phoneNumbers);

        phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);

        CFRelease(phoneNumbers);

    }

    

    NSString * firstname = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty)); //

    NSString * lastname = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)); //

    NSString * name = [firstname==nil?@"":firstname stringByAppendingString:lastname==nil?@"":lastname];

    

    if(phone.length && [phone hasPrefix:@"+86"]){

        phone = [phone substringFromIndex:3];

    }

    

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

    self.searchTField.text = phone;

    

    

    [peoplePicker dismissViewControllerAnimated:NO completion:^{}];

}

 

// Deprecated, use predicateForSelectionOfPerson and/or -peoplePickerNavigationController:didSelectPerson: instead.

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person NS_DEPRECATED_IOS(2_0, 8_0)

{

    ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);

    NSString * phone  = @"";

    if (phoneNumbers) {

        //CFIndex count = ABMultiValueGetCount(phoneNumbers);

        phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);

        CFRelease(phoneNumbers);

    }

    NSString * firstname = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty)); //

    NSString * lastname  = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)); //

    NSString * name = [firstname==nil?@"":firstname stringByAppendingString:lastname==nil?@"":lastname];

    

    NSLog(@"phone=%@",phone);

    

    if(phone.length && [phone hasPrefix:@"+86"]){

        phone = [phone substringFromIndex:3];

    }

    

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

    

    self.searchTField.text = phone;

        

    

    [peoplePicker dismissViewControllerAnimated:YES completion:^{}];

    return NO;

}

posted @ 2015-11-16 14:07  Mr_tao  阅读(298)  评论(0编辑  收藏  举报