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];
    }

運行程序,大功告成。

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM