1.導(dǎo)入頭文件
#import "ViewController.h"
#import <AddressBookUI/AddressBookUI.h>
2.代理
@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate>
@end
@implementation ViewController
3.創(chuàng)建控制器
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
ABPeoplePickerNavigationController * ab = [[ABPeoplePickerNavigationController alloc] init];
ab.peoplePickerDelegate = self;
[self presentViewController:ab animated:YES completion:nil];
}
4.調(diào)用代理方法
// Called after a person has been selected by the user.
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person NS_AVAILABLE_IOS(8_0){
CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
NSLog(@"%@---%@",firstName,lastName);
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex count = ABMultiValueGetCount(multi);
for (int index = 0; index < count; index++) {
NSString * phone = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(multi, index);
NSString * value = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(multi, index);
NSLog(@"%@",value);
}
}
// Called after a property has been selected by the user.
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0){
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex count = ABMultiValueGetCount(multi);
for (int index = 0; index < count; index++) {
NSString * phone = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(multi, index);
NSString * value = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(multi, index);
NSLog(@"%@",value);
}
}
// Called after the user has pressed cancel.
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
NSLog(@"111");
}
@end
需要注意的是:
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person NS_AVAILABLE_IOS(8_0)帘腹;
如果這個方法調(diào)用了,那么顯示詳情的方法 - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0){
就不會被調(diào)用凿将。