获取通讯录

1.#import <Contacts/Contacts.h>      

 // 授权获取通讯录

Plist 文件中添加授权权限(Privacy - Contacts Usage Description)

2.添加代码在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];


    if(authorizationStatus ==CNAuthorizationStatusNotDetermined) {


        CNContactStore*contactStore = [[CNContactStore alloc]init];


        [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted,NSError*_Nullable error) {


            if(granted) {


                NSLog(@"通讯录获取授权成功==");


                [self getContact]; //5.获取用户通讯录


            }else{


                NSLog(@"授权失败, error=%@", error);


            }


        }];


    }

}

- (void)getContact{


    CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];


    if(authorizationStatus ==CNAuthorizationStatusAuthorized) {


        // 获取指定的字段,并不是要获取所有字段,需要指定具体的字段


        NSArray*keysToFetch =@[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactPhoneNumbersKey];


        CNContactFetchRequest*fetchRequest = [[CNContactFetchRequest alloc]initWithKeysToFetch:keysToFetch];


        CNContactStore*contactStore = [[CNContactStore alloc]init];


        //创建一个保存通讯录的数组


        NSMutableArray *contactArr = [NSMutableArray array];


        [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;

                NSString *nameStr = [NSString stringWithFormat:@"%@%@",contact.familyName,contact.givenName];

//                NSDictionary*contact =@{@"phone":phoneNumber.stringValue,@"user":FORMAT(@"%@%@",familyName,givenName)};

                NSDictionary*contact =@{@"phone":phoneNumber.stringValue,@"user":nameStr};

                [contactArr addObject:contact];


                NSLog(@"label=%@, phone=%@", label, phoneNumber.stringValue);


            }


            //*stop = YES;// 停止循环,相当于break;


        }];


        _contactArr= contactArr;


        NSError*error;


        NSData*jsonData = [NSJSONSerialization dataWithJSONObject:contactArr options:NSJSONWritingPrettyPrinted error:&error];//此处data参数是我上面提到的key为"data"的数组


        NSString*jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];


        _jsonString= jsonString;


        NSLog(@"jsonString====%@",jsonString);


//        [self postContactTo]; //6.上传通讯录


    }else{


        NSLog(@"====通讯录没有授权====");


    }


}


*转载记录

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。