iOS9 通讯录(有界面)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//不用授权
//展示界面
CNContactPickerViewController *picker = [[CNContactPickerViewController alloc]init];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
//- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact
//{
//
//
//}
//- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty
//{
//
//}
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact*> *)contacts
{
for (CNContact *contact in contacts) {
NSLog(@"%@",contact.givenName);
for (CNLabeledValue *value in contact.phoneNumbers) {
NSLog(@"%@",value.value);
}
}
//不用授权
//展示界面
CNContactPickerViewController *picker = [[CNContactPickerViewController alloc]init];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
//- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact
//{
//
//
//}
//- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty
//{
//
//}
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact*> *)contacts
{
for (CNContact *contact in contacts) {
NSLog(@"%@",contact.givenName);
for (CNLabeledValue *value in contact.phoneNumbers) {
NSLog(@"%@",value.value);
}
}
}
iOS9 通讯录(无界面)
/*
CNAuthorizationStatusNotDetermined = 0,
CNAuthorizationStatusRestricted,
CNAuthorizationStatusDenied,
CNAuthorizationStatusAuthorized
*/
CNContactStore *store = [[CNContactStore alloc]init];
if (CNAuthorizationStatusNotDetermined == [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]) {
//授权
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"授权成功!");
}else{
NSLog(@"授权失败!");
}
}];
}
CNAuthorizationStatusNotDetermined = 0,
CNAuthorizationStatusRestricted,
CNAuthorizationStatusDenied,
CNAuthorizationStatusAuthorized
*/
CNContactStore *store = [[CNContactStore alloc]init];
if (CNAuthorizationStatusNotDetermined == [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]) {
//授权
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"授权成功!");
}else{
NSLog(@"授权失败!");
}
}];
}
#import "ViewController.h"
#import <Contacts/Contacts.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//获取数据
CNContactStore *store = [[CNContactStore alloc]init];
//封装参数
CNContactFetchRequest *request = [[CNContactFetchRequest alloc]initWithKeysToFetch:@[CNContactGivenNameKey,CNContactPhoneNumbersKey]];
[store enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
//stop
//*stop = YES;
NSLog(@"%@",contact.givenName);
for (CNLabeledValue *value in contact.phoneNumbers) {
NSLog(@"%@",value.value);
}
}];
#import <Contacts/Contacts.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//获取数据
CNContactStore *store = [[CNContactStore alloc]init];
//封装参数
CNContactFetchRequest *request = [[CNContactFetchRequest alloc]initWithKeysToFetch:@[CNContactGivenNameKey,CNContactPhoneNumbersKey]];
[store enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
//stop
//*stop = YES;
NSLog(@"%@",contact.givenName);
for (CNLabeledValue *value in contact.phoneNumbers) {
NSLog(@"%@",value.value);
}
}];
}