• 通讯录


    iOS 8.0 无界面通讯录

    #import "ViewController.h" #import <AddressBook/AddressBook.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //获取授权 ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus(); if (status ==kABAuthorizationStatusNotDetermined) { //创建通讯录对象 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); //授权回调 ABAddressBookRequestAccessWithCompletion(book, ^(bool granted, CFErrorRef error) { if (granted) { NSLog(@"成功"); }else{ NSLog(@"失败"); } }); } } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //创建通讯录 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); //获取所有联系人信息 CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(book); CFIndex count = CFArrayGetCount(allPeople); for (CFIndex i = 0; i< count; i++) { //根据索引,获取一条联系人记录 ABRecordRef record = CFArrayGetValueAtIndex(allPeople, i); //获取用户电话(因为用户可以用多个电话, 也有多种类型的电话) ABMultiValueRef 是一个集合类型 ABMultiValueRef mulitiValue = ABRecordCopyValue(record, kABPersonPhoneProperty); //遍历集合 for (CFIndex i = 0; i<ABMultiValueGetCount(mulitiValue); i++) { //根据索引获取电话 CFStringRef str = ABMultiValueCopyValueAtIndex(mulitiValue, i); //(__bridge_transfer NSString *)str; 桥接, 让CF字符串 转为NSString 让ARC 进行管理 NSString * phone = (__bridge_transfer NSString *)str; NSLog(@"%@" , phone); } } }

     iOS 8.0 有界面通讯录: 导入<AddressBookUI/AddressBookUI.h>

    #import "ViewController.h"
    #import <AddressBookUI/AddressBookUI.h>
    
    
    @interface ViewController ()<ABPeoplePickerNavigationControllerDelegate>
    
    @property(nonatomic ,strong) ABPeoplePickerNavigationController * picker;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
         //实例化通讯录控制器
        ABPeoplePickerNavigationController * picker = [[ABPeoplePickerNavigationController alloc]init];
        
        //设置通讯录控制器的代理
        
        picker.peoplePickerDelegate = self;
    
        
        self.picker = picker;
    
        
    
    }
    
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
    
        
        //modal控制器
        [self presentViewController:self.picker animated:YES completion:nil];
        
        
    }
    
    
    //如果实现peoplePickerNavigationController 那么property就无效, 就不执行
    //- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person NS_AVAILABLE_IOS(8_0){
    //    NSLog(@"didSelectPerson");
    //    
    //
    //    CFStringRef firstName =  ABRecordCopyValue(person, kABPersonFirstNameProperty);
    //    
    //    CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
    //    
    //    NSString * firstStr = (__bridge_transfer NSString*)firstName;
    //    
    //    NSString * lastStr = (__bridge_transfer NSString*)lastName;
    //    
    //    NSLog(@"%@  ,%@ " ,firstStr , lastStr );
    //    
    //    
    //    
    //    ABMultiValueRef multiValue = ABRecordCopyValue(person, kABPersonPhoneProperty);
    //    
    //    CFIndex count =  ABMultiValueGetCount(multiValue);
    //    
    //    
    //    for (CFIndex i = 0; i<count; i++) {
    //        
    //        CFStringRef str = ABMultiValueCopyValueAtIndex(multiValue, i);
    //        
    //        NSString * phone  = (__bridge_transfer NSString*)str;
    //        
    //        NSLog(@"%@" , phone);
    //        
    //    }
        
        
    //}
    
    // Called after a property has been selected by the user.(当选择通讯录属性时调用,也就是可以显示详情)
    - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0){
        
        NSLog(@"property");
        
        //获取选择的属性  记住C语言的规则, 需要操作什么类型, 就拿到要操作的类型,使用Get Copy,来获取
        
        CFStringRef firstName =  ABRecordCopyValue(person, kABPersonFirstNameProperty);
        
        CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
        
        NSString * firstStr = (__bridge_transfer NSString*)firstName;
        
        NSString * lastStr = (__bridge_transfer NSString*)lastName;
        
        NSLog(@"%@  ,%@ " ,firstStr , lastStr );
        
        
        
        ABMultiValueRef multiValue = ABRecordCopyValue(person, kABPersonPhoneProperty);
        
        CFIndex count =  ABMultiValueGetCount(multiValue);
        
        
        for (CFIndex i = 0; i<count; i++) {
            
            CFStringRef str = ABMultiValueCopyValueAtIndex(multiValue, i);
            
            NSString * phone  = (__bridge_transfer NSString*)str;
            
            NSLog(@"%@" , phone);
            
        }
        //判断选择的联系人是否设置电话,不为空才释放
        if(multiValue !=NULL){
            
            CFRelease(multiValue);
        }
        
        
    }
    
    // Called after the user has pressed cancel.
    - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
        NSLog(@"peoplePickerNavigationControllerDidCancel");
        
    
    }
    

    iOS 9.0 无界面通讯录:   导入<Contacts/Contacts.h>

    #import "ViewController.h"
    #import <Contacts/Contacts.h>
    
    @interface ViewController ()
    
    @property(nonatomic ,strong) CNContactStore *store;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
      //获取授权状态
        CNAuthorizationStatus  status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    
        //创建通讯录仓库
        CNContactStore *store = [[CNContactStore alloc]init];
        
        self.store =store;
        
        //判断是否通过授权,未授权就请求授权
        if (status == CNAuthorizationStatusNotDetermined) {
            //请求授权
            [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
                
                if (granted) {
                    
                    NSLog(@"成功");
                }else{
                    NSLog(@"失败");
                }
                
                
            }];
            
        }
        
    }
    
    
    
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        
        //获取通讯录(信息)请求
        CNContactFetchRequest * requser = [[CNContactFetchRequest alloc]initWithKeysToFetch:@[CNContactGivenNameKey , CNContactPhoneNumbersKey]];
        
    
        //遍历请求
        [self.store enumerateContactsWithFetchRequest:requser error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
           
            //contact 就是通讯录数据
            NSLog(@"%@" ,contact.givenName);
            
        }];
        
    }
    

     iOS 9.0 有界面通讯录 导入<ContactsUI/ContactsUI.h>

    #import "ViewController.h"
    #import <ContactsUI/ContactsUI.h>
    
    @interface ViewController ()<CNContactPickerDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        //创建CNContactPickerViewController 控制器
        CNContactPickerViewController * picker = [[CNContactPickerViewController alloc]init];
        //设置代理
        picker.delegate = self ;
        
        //modal控制器
        [self presentViewController:picker animated:YES completion:nil];
        
        
    
    }
    
    
    //实现didSelectContact,那么didSelectContactProperty就不执行
    -(void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
        //contact 就是通讯录数据
        NSLog(@"%@" , contact.givenName);
        
    //    NSLog(@"%@" , contact.phoneNumbers);
        for (CNLabeledValue * obj in contact.phoneNumbers) {
            
            CNPhoneNumber * num = obj.value;
            
            
            
            NSLog(@"%@  ,  %@"   , num.stringValue , obj.label);
            
            
        }
        
    }
    
    
    -(void)contactPickerDidCancel:(CNContactPickerViewController *)picker{
        
    }
    
    //
    -(void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty{
        
        
    }
    

    总结://获取选择的属性 记住C语言的规则, 需要操作什么类型, 就拿到要操作的类型,使用Get Copy,来获取,

           注意:C语言 creat , copy 字样创建的对象,注意遵循谁创建,谁release

  • 相关阅读:
    #1015 : KMP算法
    #1014 Trie树
    Type.IsContextful 说明
    判断.net中在windows系统下的字节序
    Python3 循环语句
    adb 脚本
    如何使用 adb 命令实现自动化测试
    python 字符串的方法和注释
    Android使用Fiddler模拟弱网络环境测试
    Android定位元素与操作
  • 原文地址:https://www.cnblogs.com/yuwei0911/p/5426133.html
Copyright © 2020-2023  润新知