標籤:
昨天因項目需求要訪問系統通訊錄擷取電話號碼,於是乎從一無所知,開始倒騰,倒騰了一下午,總算了弄好了。寫這邊部落格是為了記錄一下,自己下一次弄的時候就別在出錯了。同時,有和我一樣的菜鳥能夠避免走一下彎路。
好了,言歸正傳,要訪問系統的通訊錄,首先需要添加AddressBook.framework和AddressBookUI.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系統通訊錄使用