#import "ViewController.h"
//iOS6开始, 要访问通讯录, 必须先授权. 不授权上架可能被拒
//AddressBook是iOS9之前用的类库. 使用的C语言的底层语法. 用的是CoreFoundation.
//#import<AddressBook/AddressBook.h>
//#import<AddressBook/AddressBookUI.h>
//iOS9出的. 面向对象. 苹果官方推荐使用ContactsUI.
#import<Contacts/Contacts.h>
#import<Contacts/ContactsUI.h>
@interface ViewController ()<CNContactPickerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event
{
//1. 创建联系人选择控制器
CNContactPickerViewController *pickerVC = [CNContactPickerViewController new];
//2. 设置代理 --> 获取数据
pickerVC.delegate = self;
//3. 模态弹出
[self presentViewController:pickerVC animated:YES completion:nil];
}
#pragma mark CNContactPickerViewController的代理方法
// 当选中联系人之后会调用此方法
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{ //1. 姓名 NSLog(@" 姓: %@, 名: %@", contact.familyName, contact.givenName); //2. 电话 //NSArray*> *phoneNumbers;
for (CNLabeledValue *labeledValue in contact.phoneNumbers) {
// 通过打印(泛型)可以得知类型 ValueType = CNPhoneNumber
CNPhoneNumber *phoneNumber = labeledValue.value;
NSLog(@"labeled: %@", phoneNumber.stringValue);
}
}
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker
{
}