1. 程式人生 > >ABPeoplePickerNavigationController 獲取手機通訊錄資訊

ABPeoplePickerNavigationController 獲取手機通訊錄資訊

最近專案用到獲取使用者手機通訊錄的功能,但發現有兩個代理方法在iOS 9.0 廢棄了,用新的代理方法代替,所以整理下供以後參考。。

#pragma mark - 點選聯絡人 連結到使用者手機通訊錄

- (void)accessPhoneBook: (UIButton *)sender {
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc]init];
    picker.peoplePickerDelegate = self;

    [picker setHidesBottomBarWhenPushed:YES];
    [self presentViewController:picker animated:YES completion:nil];
}

- (void)displayPerson:(ABRecordRef)person {
    NSString* phone = nil;
    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,
                                                     
                                                     kABPersonPhoneProperty);
    
    if (ABMultiValueGetCount(phoneNumbers) > 0) {
        phone = (__bridge_transfer NSString*)
        ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
    }
    else {
        phone = @"[None]";
        
    }
    
    self.textFieldView.text = phone;
    self.textFieldView.text = [self.textFieldView.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    self.phoneNum = [phone stringByReplacingOccurrencesOfString:@"-" withString:@""];

    CFRelease(phoneNumbers);
}

#pragma mark - ABPeoplePickerNavigationControllerDelegate - iOS 9.0 new
// iOS 9.0 new
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person {
    
    [self displayPerson:person];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

// iOS 9.0 new
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
    
}


- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
    [self dismissViewControllerAnimated:YES completion:nil];

}
#pragma mark

PS 之前被廢棄的代理方法是:

// Deprecated, use predicateForSelectionOfPerson and/or -peoplePickerNavigationController:didSelectPerson: instead.
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person NS_DEPRECATED_IOS(2_0, 8_0);

// Deprecated, use predicateForSelectionOfProperty and/or -peoplePickerNavigationController:didSelectPerson:property:identifier: instead.
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_DEPRECATED_IOS(2_0, 8_0);