美文网首页
访问系统通讯录contact iOS9以后的API 附:两个较

访问系统通讯录contact iOS9以后的API 附:两个较

作者: AnnieAri | 来源:发表于2017-01-13 15:58 被阅读0次

一 . 带UI

#import <ContactsUI/ContactsUI.h>

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //1. 创建联系人控制器   
 CNContactPickerViewController *contactPickerVC = [[CNContactPickerViewController alloc] init];     
  //2. 设置代理 -->获取数据  
  contactPickerVC.delegate = self;  
  //3.模态弹出视图 -->   
 [self presentViewController:contactPickerVC animated:true completion:nil];
}
#pragma mark - 通讯录代理方法/**点击取消按钮 会调用*/- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker {    NSLog(@"cancel");}/**
选择某个联系人会调用 - contact头文件中有详细的电话信息*/
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
      //1. 获取姓名
    NSLog(@"xing: %@, ming: %@",contact.familyName,contact.givenName);
     //2. 获取电话
    //NSArray<CNLabeledValue<CNPhoneNumber *> *>   
 for (CNLabeledValue *labeledValue in contact.phoneNumbers) {    
    CNPhoneNumber *phoneNumber = labeledValue.value;      
  NSLog(@"phone: %@",phoneNumber.stringValue);
    }
}
/**选中多个联系人的方法  -- 暂时没什么用  这个方法如果实现  选择单个联系人的方法会被忽略!!!*/
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact *> *)contacts {   
}

二.不带UI

#import <Contacts/Contacts.h>

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {   
 /**    CNAuthorizationStatusNotDetermined = 0,    
   CNAuthorizationStatusRestricted,    
   CNAuthorizationStatusDenied,  
  CNAuthorizationStatusAuthorized     */   
 //1. 获取授权状态 
   CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];  
  //2.处理未决定状态  
 if (status == CNAuthorizationStatusNotDetermined) {     
   [[CNContactStore new] requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {      
     //2.1 判断授权成功          
  if (granted) {            
    //2.2 获取数据           
     [self onGetContactInfo];    
        }else {           
     NSLog(@"授权失败");       
     }      
  }];      
  return;   
 }   
//3 处理其他情况  
  if (status == CNAuthorizationStatusAuthorized) {    
    [self onGetContactInfo];    
    return;  
  }else {   
    NSLog(@"请在设置中打开");  
  }
}
#pragma mark - 获取数据的方法
- (void)onGetContactInfo {   
 //二. 获取信息  
  //1.联系人信息抓取请求  
  //cnkey :联系人信息是由多个属性组成的  姓/名/电话  每个属性都有一个字符串的key   
 CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactPhoneNumbersKey]];  
  //2.根据请求枚举数据   
 CNContactStore *contactStore = [CNContactStore new];
    [contactStore enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {     
   NSLog(@"%@ : %@",contact.givenName,contact.familyName);  
      for (CNLabeledValue *labeledValue in contact.phoneNumbers) {     
       CNPhoneNumber *phoneNumber = labeledValue.value;      
      NSLog(@"tel: %@",phoneNumber.stringValue);      
  }   
 }];
}

附:
适配iOS8的两个类库- AddressBookUI.AddressBook

#import <AddressBookUI/AddressBookUI.h>

#pragma mark - 显示联系人界面
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {   
 //1.创建联系人选择导航控制器    
ABPeoplePickerNavigationController *picker = [ABPeoplePickerNavigationController new];    
//2.设置代理 - 一定不要写delegate  
  picker.peoplePickerDelegate = self;  
  //3.模态弹出控制器   
 [self presentViewController:picker animated:true completion:nil];
}
#pragma mark - 代理方法
/**选中联系人会调用的方法*/
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person {

    CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);

     //ABAddressBook框架,使用的是coreFoundation的语法 没有arc ,所以遇到的Copy/alloc/create  需要释放

    CFRelease(firstName);

}
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {   

}

相关文章

网友评论

      本文标题:访问系统通讯录contact iOS9以后的API 附:两个较

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