iOS:ABPeoplePickerNavigationController系统通讯录使用

  昨天因项目需求要访问系统通讯录获取电话号码,于是乎从一无所知,开始倒腾,倒腾了一下午,总算了弄好了。写这边博客是为了记录一下,自己下一次弄的时候就别在出错了。同时,有和我一样的菜鸟能够避免走一下弯路。

  好了,言归正传,要访问系统的通讯录,首先需要添加AddressBook.frameworkAddressBookUI.framework两个框架到你工程中build phase的"Link Binary With Libraries"之下,然后就可以开始了。

  首先我们需要创建一个控制器:ViewController,在.h文件中导入头文件:<AddressBook/AddressBook.h>、 <AddressBookUI/AddressBookUI.h>,

#import <AddressBook/AddressBook.h>

#import <AddressBookUI/AddressBookUI.h>

然后在控制器实现ABPeoplePickerNavigationControllerDelegate, UINavigationControllerDelegate协议

@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate, UINavigationControllerDelegate>

在viewDidAppear方法中创建ABPeoplePickerNavigationController,同时设置viewController作为委托对象

- (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出现了坑爹的问题,就是选择联系人后

ABPeoplePickerNavigationController会自动dismiss掉,这个问题可坑坏我了。问了谷歌和度娘,在stackvoerflow找到了类似的问题,但是都没有得到解决,在觉得没有办法的时候,又开始看ABPeoplePickerNavigationController.h的头文件,发现了

predicateForSelectionOfPerson属性,于是乎在viewDidAppear方法中加入如下代码:

 if([[UIDevice currentDevice].systemVersion floatValue] >= 8.0){

        pNC.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];

    }

运行程序,大功告成。

  

 

你可能感兴趣的:(controller)