獲取通訊錄-AddressBookUI

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);
    如果這個(gè)方法調(diào)用了,那么顯示詳情的方法
  • (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0){
    就不會(huì)被調(diào)用。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容