iOS实现封装一个获取通讯录的工具类详解

 更新时间:2017年10月31日 09:32:39   作者:hello老文  
这篇文章主要给大家介绍了关于iOS如何实现封装一个获取通讯录的工具类的相关资料,这是自己平时封装的一个工具类,使用非常方便,文中给出了详细的示例代码,需要的朋友们可以参考借鉴,下面随着小编来一起学习学习吧。

前言

本文给大家介绍了关于iOS如何封装一个获取通讯录工具类的相关内容,iOS获取通讯录一共有4个framework: AddressBook, AddressBookUI, Contacts, ContactsUI; 其中 AddressBook 和 AddressBookUI 已经被iOS9时 deprecated 了, 而推出了Contacts 和 ContactsUI 取代之. 其中 AddressBookUI 和 ContactsUI 是picker出一个界面提供选择一条联系人信息并且是不需要手动授权, AddressBook 和 Contacts 是获取全部通讯录数据并且需要手动授权.下面来一起看看详细的介绍吧。

注意:在iOS10获取通讯录权限需主动在info.plist里添加上提示信息. 不然会崩溃. 在info.plist里添加一对key和value

  • key: Privacy - Contacts Usage Description
  • value: 自由发挥, 这里随便写一句: 是否允许此App访问你的通讯录?

ContactsModel

新建两个数据模型文件来保存获取的通讯录数据

ContactsModel.h

#import <Foundation/Foundation.h>

@interface ContactsModel : NSObject
@property (nonatomic, copy) NSString *num;
@property (nonatomic, copy) NSString *name;

- (instancetype)initWithName:(NSString *)name num:(NSString *)num;
@end

ContactsModel.m

#import "ContactsModel.h"

@implementation ContactsModel

- (instancetype)initWithName:(NSString *)name num:(NSString *)num {
 if (self = [super init]) {
  self.name = name;
  self.num = num;
 }
 return self;
}

@end

ContactsHelp

这是获取通讯录的工具类.

ContactsHelp.h

#import <UIKit/UIKit.h>
#import "ContactsModel.h"

typedef void(^ContactBlock)(ContactsModel *contactsModel);

@interface ContactsHelp : NSObject

+ (NSMutableArray *)getAllPhoneInfo;

- (void)getOnePhoneInfoWithUI:(UIViewController *)target callBack:(ContactBlock)block;

@end

ContactsHelp.m

#import "ContactsHelp.h"
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>

#define iOS9 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0)

@interface ContactsHelp () <CNContactPickerDelegate, ABPeoplePickerNavigationControllerDelegate>
@property(nonatomic, strong) ContactsModel *contactModel;
@property(nonatomic, strong) ContactBlock myBlock;
@end

@implementation ContactsHelp

+ (NSMutableArray *)getAllPhoneInfo {
 return iOS9 ? [self getContactsFromContacts] : [self getContactsFromAddressBook];
}

- (void)getOnePhoneInfoWithUI:(UIViewController *)target callBack:(void (^)(ContactsModel *))block {
 if (iOS9) {
  [self getContactsFromContactUI:target];
 } else {
  [self getContactsFromAddressBookUI:target];
 }
 self.myBlock = block;
}

#pragma mark - AddressBookUI
- (void)getContactsFromAddressBookUI:(UIViewController *)target {
 ABPeoplePickerNavigationController *pickerVC = [[ABPeoplePickerNavigationController alloc] init];
 pickerVC.peoplePickerDelegate = self;
 [target presentViewController:pickerVC animated:YES completion:nil];
}

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person {
 ABMultiValueRef phonesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
 if (!phonesRef) { return; }
 NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phonesRef, 0);

 CFStringRef lastNameRef = ABRecordCopyValue(person, kABPersonLastNameProperty);
 CFStringRef firstNameRef = ABRecordCopyValue(person, kABPersonFirstNameProperty);
 NSString *lastname = (__bridge_transfer NSString *)(lastNameRef);
 NSString *firstname = (__bridge_transfer NSString *)(firstNameRef);
 NSString *name = [NSString stringWithFormat:@"%@%@", lastname == NULL ? @"" : lastname, firstname == NULL ? @"" : firstname];
 NSLog(@"姓名: %@", name);

 ContactsModel *model = [[ContactsModel alloc] initWithName:name num:phoneValue];
 NSLog(@"电话号码: %@", phoneValue);

 CFRelease(phonesRef);
 if (self.myBlock) self.myBlock(model);
}

#pragma mark - ContactsUI
- (void)getContactsFromContactUI:(UIViewController *)target {
 CNContactPickerViewController *pickerVC = [[CNContactPickerViewController alloc] init];
 pickerVC.delegate = self;
 [target presentViewController:pickerVC animated:YES completion:nil];
}

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
 NSString *name = [NSString stringWithFormat:@"%@%@", contact.familyName == NULL ? @"" : contact.familyName, contact.givenName == NULL ? @"" : contact.givenName];
 NSLog(@"姓名: %@", name);

 CNPhoneNumber *phoneNumber = [contact.phoneNumbers[0] value];
 ContactsModel *model = [[ContactsModel alloc] initWithName:name num:[NSString stringWithFormat:@"%@", phoneNumber.stringValue]];
 NSLog(@"电话号码: %@", phoneNumber.stringValue);

 if (self.myBlock) self.myBlock(model);
}

#pragma mark - AddressBook
+ (NSMutableArray *)getContactsFromAddressBook {
 ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
 CFErrorRef myError = NULL;
 ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &myError);
 if (myError) {
  [self showErrorAlert];
  if (addressBook) CFRelease(addressBook);
  return nil;
 }

 __block NSMutableArray *contactModels = [NSMutableArray array];
 if (status == kABAuthorizationStatusNotDetermined) { // 用户还没有决定是否授权你的程序进行访问
  ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
   if (granted) {
    contactModels = [self getAddressBookInfo:addressBook];
   } else {
    [self showErrorAlert];
    if (addressBook) CFRelease(addressBook);
   }
  });
  // 用户已拒绝 或 iOS设备上的家长控制或其它一些许可配置阻止程序与通讯录数据库进行交互
 } else if (status == kABAuthorizationStatusDenied || status == kABAuthorizationStatusRestricted) {
  [self showErrorAlert];
  if (addressBook) CFRelease(addressBook);
 } else if (status == kABAuthorizationStatusAuthorized) { // 用户已授权
  contactModels = [self getAddressBookInfo:addressBook];
 }
 return contactModels;
}

+ (NSMutableArray *)getAddressBookInfo:(ABAddressBookRef)addressBook {
 CFArrayRef peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook);
 NSInteger peopleCount = CFArrayGetCount(peopleArray);
 NSMutableArray *contactModels = [NSMutableArray array];

 for (int i = 0; i < peopleCount; i++) {
  ABRecordRef person = CFArrayGetValueAtIndex(peopleArray, i);
  ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
  if (phones) {
   NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
   NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
   NSString *name = [NSString stringWithFormat:@"%@%@", lastName == NULL ? @"" : lastName, firstName == NULL ? @"" : firstName];
   NSLog(@"姓名: %@", name);

   CFIndex phoneCount = ABMultiValueGetCount(phones);
   for (int j = 0; j < phoneCount; j++) {
    NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, j);
    NSLog(@"电话号码: %@", phoneValue);
    ContactsModel *model = [[ContactsModel alloc] initWithName:name num:phoneValue];
    [contactModels addObject:model];
   }
  }
  CFRelease(phones);
 }

 if (addressBook) CFRelease(addressBook);
 if (peopleArray) CFRelease(peopleArray);

 return contactModels;
}


#pragma mark - Contacts
+ (NSMutableArray *)getContactsFromContacts {
 CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
 CNContactStore *store = [[CNContactStore alloc] init];
 __block NSMutableArray *contactModels = [NSMutableArray array];

 if (status == CNAuthorizationStatusNotDetermined) { // 用户还没有决定是否授权你的程序进行访问
  [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
   if (granted) {
    contactModels = [self getContactsInfo:store];
   } else {
    [self showErrorAlert];
   }
  }];
  // 用户已拒绝 或 iOS设备上的家长控制或其它一些许可配置阻止程序与通讯录数据库进行交互
 } else if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted) {
  [self showErrorAlert];
 } else if (status == CNAuthorizationStatusAuthorized) { // 用户已授权
  contactModels = [self getContactsInfo:store];
 }

 return contactModels;
}

+ (NSMutableArray *)getContactsInfo:(CNContactStore *)store {
 NSArray *keys = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
 CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
 NSMutableArray *contactModels = [NSMutableArray array];

 [store enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
  NSString *name = [NSString stringWithFormat:@"%@%@", contact.familyName == NULL ? @"" : contact.familyName, contact.givenName == NULL ? @"" : contact.givenName];
  NSLog(@"姓名: %@", name);

  for (CNLabeledValue *labeledValue in contact.phoneNumbers) {
   CNPhoneNumber *phoneNumber = labeledValue.value;
   NSLog(@"电话号码: %@", phoneNumber.stringValue);
   ContactsModel *model = [[ContactsModel alloc] initWithName:name num:phoneNumber.stringValue];
   [contactModels addObject:model];
  }
 }];

 return contactModels;
}

#pragma mark - Error
+ (void)showErrorAlert {
 NSLog(@"授权失败, 请允许app访问您的通讯录, 在手机的”设置-隐私-通讯录“选项中设置允许");
}

@end

使用

#import "ContactsHelp.h"
#import "ContactsModel.h"

...

@property(nonatomic, strong) ContactsHelp *contactsHelp;

...

- (IBAction)btn_getOne {
 self.contactsHelp = [[ContactsHelp alloc] init];
 [self.contactsHelp getOnePhoneInfoWithUI:self callBack:^(ContactsModel *contactModel) {
  NSLog(@"-----------");
  NSLog(@"%@", contactModel.name);
  NSLog(@"%@", contactModel.num);
 }];
}

- (IBAction)btn_getAll {
 NSMutableArray *contactModels = [ContactsHelp getAllPhoneInfo];
 [contactModels enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  ContactsModel *model = obj;
  NSLog(@"-----------");
  NSLog(@"%@", model.name);
  NSLog(@"%@", model.num);
 }];
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

相关文章

  • iOS实现微信朋友圈视频截取功能

    iOS实现微信朋友圈视频截取功能

    这篇文章主要介绍了iOS实现微信朋友圈视频截取功能,微信使用非常普遍,功能也很强大,不知道大家对微信朋友圈视频截取功能有没有了解,下面脚本之家小编给大家带来详解介绍,感兴趣的朋友一起看看吧
    2018-07-07
  • iOS 请求权限封装类的实例代码

    iOS 请求权限封装类的实例代码

    下面小编就为大家分享一篇iOS 请求权限封装类的实例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • IOS开发笔记整理49之详解定位CLLocation

    IOS开发笔记整理49之详解定位CLLocation

    在项目功能中有一个定位CLLocation的需求,遇到了一些知识难点,经过各位大侠的帮助,问题解决,特此分享供大家学习,希望大家共同学习进步
    2015-11-11
  • c/c++堆栈分布及其设置方法

    c/c++堆栈分布及其设置方法

    这篇文章主要介绍了c/c++堆栈分布及其设置方法,需要的朋友可以参考下
    2014-01-01
  • iOS中使用对象的弱引用示例代码

    iOS中使用对象的弱引用示例代码

    这篇文章主要给大家介绍了关于iOS中使用对象的弱引用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-01-01
  • Xcode中iOS应用开发的一般项目目录结构和流程简介

    Xcode中iOS应用开发的一般项目目录结构和流程简介

    这篇文章主要介绍了Xcode中iOS应用开发的一般项目目录结构和流程简介,包括项目所需的一些平台路径如模拟器路径等的介绍,需要的朋友可以参考下
    2016-02-02
  • IOS Ble蓝牙开发实现方法

    IOS Ble蓝牙开发实现方法

    这篇文章主要为大家详细介绍了IOS Ble蓝牙开发的实现方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12
  • IOS实现输入验证码、密码按位分割(二)

    IOS实现输入验证码、密码按位分割(二)

    这篇文章主要介绍了IOS实现输入验证码、密码按位分割的方法,在App内,密码及验证码的输入,采用按位输入的方法,且位与位之间有分隔线,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • iOS UICollectionView实现横向滑动

    iOS UICollectionView实现横向滑动

    这篇文章主要为大家详细介绍了iOS UICollectionView实现横向滑动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-03-03
  • iOS 汉字的拼音

    iOS 汉字的拼音

    本文通过一段代码给代码介绍了ios汉字的拼音知识及将汉字转拼音的代码,非常不错,具有参考借鉴价值,感兴趣的朋友一起看下吧
    2016-08-08

最新评论