ViewController.h
#import <AddressBookUI/AddressBookUI.h>
#import <AddressBook/AddressBook.h>
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate>
@end
ViewController.m
@implementation ViewController
//弹出 通讯录 拭去 UI 的方法
-(void)showAddressBookBtnClicked:(UIButton*)sender{
sender.enabled = NO;
__block UIButton * bBtn = sender;
ABPeoplePickerNavigationController * peoplePickerNav = [[ABPeoplePickerNavigationControlleralloc] init];
NSArray *displayedItems =@[[NSNumbernumberWithInt:kABPersonPhoneProperty]]; // 只显示 电话号码
peoplePickerNav.displayedProperties = displayedItems;
peoplePickerNav.peoplePickerDelegate = self;
[self.navigationControllerpresentViewController:peoplePickerNav animated:YEScompletion:^{
bBtn.enabled = YES;
}];
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
[peoplePicker dismissViewControllerAnimated:YEScompletion:nil];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
return YES;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
ABPropertyType propertyType =ABPersonGetTypeOfProperty(property);
if (propertyType ==kABMultiStringPropertyType) {
CFTypeRef phoneProperty = ABRecordCopyValue(person, property);
int index = ABMultiValueGetIndexForIdentifier(phoneProperty, identifier);
CFTypeRef value = ABMultiValueCopyValueAtIndex(phoneProperty,index);
NSString * valueString = (__bridge_transferNSString*)value;
NSString * pureNumbers = [[valueStringcomponentsSeparatedByCharactersInSet:[[NSCharacterSetcharacterSetWithCharactersInString:@"0123456789"]invertedSet]] componentsJoinedByString:@""]; // 将 字符串中可能的 非 号码 字符 剔除
NSLog(@"phoneNumber %@ ",pureNumbers); //最终所得到的 电话号码
}
[peoplePicker dismissViewControllerAnimated:YEScompletion:nil];
return NO;
}
@end