iOS:ABPeoplePickerNavigationController系統通訊錄使用

來源:互聯網
上載者:User

標籤:

  昨天因項目需求要訪問系統通訊錄擷取電話號碼,於是乎從一無所知,開始倒騰,倒騰了一下午,總算了弄好了。寫這邊部落格是為了記錄一下,自己下一次弄的時候就別在出錯了。同時,有和我一樣的菜鳥能夠避免走一下彎路。

  好了,言歸正傳,要訪問系統的通訊錄,首先需要添加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掉,這個問題可坑壞我了。問了Google和度娘,在stackvoerflow找到了類似的問題,但是都沒有得到解決,在覺得沒有辦法的時候,又開始看ABPeoplePickerNavigationController.h的標頭檔,發現了

predicateForSelectionOfPerson屬性,於是乎在viewDidAppear方法中加入如下代碼:

 if([[UIDevice currentDevice].systemVersion floatValue] >= 8.0){        pNC.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];    }

運行程式,大功告成。

  

 

iOS:ABPeoplePickerNavigationController系統通訊錄使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.