123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- //
- // phonePeopleVC.m
- // LN_School
- //
- // Created by apple on 2017/4/5.
- // Copyright © 2017年 Danson. All rights reserved.
- //
- #import "phonePeopleVC.h"
- #import <Contacts/Contacts.h>
- @interface phonePeopleVC ()
- {
- NSMutableArray * _dataArr;
- }
- @end
- @implementation phonePeopleVC
- #pragma mark 通讯录
- -(void)viewDidLoad{
-
- [super viewDidLoad];
-
- [self requestAuthorizationForAddressBook];
- }
- - (void)requestAuthorizationForAddressBook {
- CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
- if (authorizationStatus == CNAuthorizationStatusNotDetermined) {
- CNContactStore *contactStore = [[CNContactStore alloc] init];
- [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
- if (granted) {
-
- // 获取指定的字段,并不是要获取所有字段,需要指定具体的字段
- NSArray *keysToFetch = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
- CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch];
- CNContactStore *contactStore = [[CNContactStore alloc] init];
- [contactStore enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
- // NSLog(@"-------------------------------------------------------");
- NSString *givenName = contact.givenName;
- NSString *familyName = contact.familyName;
- // NSLog(@"givenName=%@, familyName=%@", givenName, familyName);
-
-
- NSArray *phoneNumbers = contact.phoneNumbers;
- for (CNLabeledValue *labelValue in phoneNumbers) {
- NSString *label = labelValue.label;
- CNPhoneNumber *phoneNumber = labelValue.value;
-
- // NSLog(@"label=%@, phone=%@", label, phoneNumber.stringValue);
- }
-
- // *stop = YES; // 停止循环,相当于break;
- }];
-
- } else {
- // NSLog(@"授权失败, error=%@", error);
- UIAlertController *alertFind = [UIAlertController alertControllerWithTitle:nil message:@"没有打开通讯录授权(设置-隐私-通讯录-极速驾培)" preferredStyle:UIAlertControllerStyleAlert];
-
- [alertFind addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
-
- }]];
-
- [self presentViewController:alertFind animated:true completion:nil];
- }
- }];
- }
- }
- -(void)didReceiveMemoryWarning{
- [super didReceiveMemoryWarning];
- }
- @end
|