获取通讯录-AddressBookUI

1.导入头文件

#import "ViewController.h"
#import 

2.代理

@interface ViewController ()

@end
@implementation ViewController

3.创建控制器

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    
    ABPeoplePickerNavigationController * ab = [[ABPeoplePickerNavigationController alloc] init];
    
    ab.peoplePickerDelegate = self;
    
    [self presentViewController:ab animated:YES completion:nil];
    
}

4.调用代理方法

// 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);
    如果这个方法调用了,那么显示详情的方法
  • (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0){
    就不会被调用。

你可能感兴趣的:(获取通讯录-AddressBookUI)