iOS获取通讯录问题

来源:互联网 发布:mac 无线键盘 编辑:程序博客网 时间:2024/05/16 01:22

一、iOS9之后

         

   1)调用系统通讯录

头文件:#import<ContactsUI/ContactsUI.h>

代理:CNContactPickerDelegate

#import "ViewController.h"

    //1.创建选择联系人的界面

    CNContactPickerViewController *cpvc = [[CNContactPickerViewControlleralloc] init];

    // 2.设置代理

    cpvc.delegate = self;

    // 3.弹出控制器

    [selfpresentViewController:cpvc animated:YEScompletion:nil];


//代理方法 

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{

    // 1.获取联系人的姓名

    NSString *lastname = contact.familyName;

    NSString *firstname = contact.givenName;

    NSLog(@"%@ %@", lastname, firstname);

    

    // 2.获取电话号码

    for (CNLabeledValue *labelValuein contact.phoneNumbers) {

        // 3.获取电话的label/value

        NSString *phoneLabel = labelValue.label;

        CNPhoneNumber *phoneNumber = labelValue.value;

        NSString *phoneValue = phoneNumber.stringValue;

        NSLog(@"%@ %@", phoneLabel, phoneValue);

    }

}

   //当用户选中某一个联系人的某一个属性时候会执行该方法

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty

{

    NSLog(@"%s", __func__);

}

    (2)自定义界面

     头文件:#import <Contacts/Contacts.h>

     #import "AppDelegate.h"

     //1.获取授权状态

    CNAuthorizationStatus status=[CNContactStoreauthorizationStatusForEntityType:CNEntityTypeContacts];

    NSLog(@"status=%ld",(long)status);

    //2.判定授权状态

    //2.1用户尚未作出有关授权

    if (status==CNAuthorizationStatusNotDetermined) {

        

        //3.请求授权

        //3.1创建CNContactStore

        CNContactStore*store=[[CNContactStorealloc]init];

        

        //3.2请求授权

        [store requestAccessForEntityType:CNEntityTypeContactscompletionHandler:^(BOOL granted,NSError * _Nullable error) {

            

            if (error) {

                NSLog(@"error:%@",error);

                return;

            }

            if (granted) {

                NSLog(@"授权成功");

            }else{

                NSLog(@"授权失败");

            }

        }];

    }

   #import "ViewController.h"

    //1.判断授权状态

    CNAuthorizationStatus status=[CNContactStoreauthorizationStatusForEntityType:CNEntityTypeContacts];

    if (status !=CNAuthorizationStatusAuthorized) {

        //2.如果已授权,创建通讯录对象

        CNContactStore*store=[[CNContactStorealloc]init];

        //3.请求所有的联系人

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

        CNContactFetchRequest*request=[[CNContactFetchRequestalloc]initWithKeysToFetch:keys];

        //4.请求所有联系人

        NSError*error=nil;

        [store enumerateContactsWithFetchRequest:request error:&errorusingBlock:^(CNContact *_Nonnull contact,BOOL *_Nonnull stop){

            //5.获取联系人

            //5.1获取姓名

            NSString*firstName=contact.givenName;

            NSString*lastName=contact.familyName;

            NSLog(@"firstName=%@,lastName=%@",firstName,lastName);

            //5.2获取电话号码

            NSArray*phones=contact.phoneNumbers;

            for (CNLabeledValue*labelValuein phones) {

                NSString*phoneLabel=labelValue.value;

                CNPhoneNumber*phoneNumber=labelValue.value;

                NSString*phoneValue=phoneNumber.stringValue;

                NSLog(@"phoneLabel=%@,phoneNumber=%@,phoneValue=%@",phoneLabel,phoneNumber,phoneValue);

            }

        }];

    }


0 0
原创粉丝点击