美文网首页
获取通讯录

获取通讯录

作者: Qy_iOS | 来源:发表于2018-06-14 10:44 被阅读0次

    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(@"====通讯录没有授权====");

        }

    }

    *转载记录

    相关文章

      网友评论

          本文标题:获取通讯录

          本文链接:https://www.haomeiwen.com/subject/ynieeftx.html