iOS:ABPeoplePickerNavigationController系統通信錄使用

  昨天因項目需求要訪問系統通信錄獲取電話號碼,因而乎從一無所知,開始倒騰,倒騰了一下午,總算了弄好了。寫這邊博客是爲了記錄一下,本身下一次弄的時候就別在出錯了。同時,有和我同樣的菜鳥可以避免走一下彎路。框架

  好了,言歸正傳,要訪問系統的通信錄,首先須要添加AddressBook.frameworkAddressBookUI.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

相關文章
相關標籤/搜索