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

  

  

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

  

在iOS10获取通讯录权限需主动在<代码> info.plist> info.plist>   

      <李>键:隐私-联系人使用描述李   <李>值:自由发挥,这里随便写一句:是否允许此应用访问你的通讯录& # 63;李   
  

  

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

  

ContactsModel.h         # import & lt;基?Foundation.h>      @ interface ContactsModel: NSObject   @ property(原子、复制)NSString * num;   @ property(原子、复制)NSString *名称;      - (instancetype) initWithName:(NSString *)名称num:全国矿工工会(NSString *);   @end      

ContactsModel.m         #进口“ContactsModel.h”      @ implementation ContactsModel      - (instancetype) initWithName:(NSString *)名称num:全国矿工工会(NSString *) {   如果(自我=(超级init)) {   self.name=名称;   自我。num=num;   }   回归自我;   }      @end      

  

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

  

ContactsHelp.h         # import & lt; UIKit/UIKit.h>   #进口“ContactsModel.h”      typedef无效(^ ContactBlock) (ContactsModel * ContactsModel);      @ interface ContactsHelp: NSObject      + (NSMutableArray *) getAllPhoneInfo;      - (void) getOnePhoneInfoWithUI:(*) ui目标回调:(ContactBlock)块;      @end      

ContactsHelp.m         #进口“ContactsHelp.h”   # import & lt; AddressBook/AddressBook.h>   # import & lt; AddressBookUI/AddressBookUI.h>   # import & lt;联系人/Contacts.h>   # import & lt; ContactsUI/ContactsUI.h>      #定义iOS9 ([[[UIDevice currentDevice] systemVersion] floatValue]祝辞=9.0)      @ interface ContactsHelp () & lt; CNContactPickerDelegate ABPeoplePickerNavigationControllerDelegate>   @ property(原子、强)ContactsModel * contactModel;   @ property(原子、强)ContactBlock myBlock;   @end      @ implementation ContactsHelp      + (NSMutableArray *) getAllPhoneInfo {   返回iOS9 & # 63;[自我getContactsFromContacts]:[自我getContactsFromAddressBook];   }      - (void) getOnePhoneInfoWithUI:(*) ui目标回调:(空白(^)(ContactsModel *)){块   如果(iOS9) {   (自我getContactsFromContactUI:目标);   其他}{   (自我getContactsFromAddressBookUI:目标);   }   自我。myBlock=块;   }      # pragma马克- AddressBookUI   - (void) getContactsFromAddressBookUI:目标(ui *) {   ABPeoplePickerNavigationController * pickerVC=[[ABPeoplePickerNavigationController alloc] init);   pickerVC。peoplePickerDelegate=自我;   (目标presentViewController: pickerVC动画:是的完成:nil);   }      - (void) peoplePickerNavigationController: (ABPeoplePickerNavigationController *) peoplePicker didSelectPerson: {(ABRecordRef)人   ABMultiValueRef phonesRef=ABRecordCopyValue(人,kABPersonPhoneProperty);   如果(! phonesRef){返回;}   NSString * phoneValue=https://www.yisu.com/zixun/(__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex (phonesRef 0);      CFStringRef lastNameRef=ABRecordCopyValue(人,kABPersonLastNameProperty);   CFStringRef firstNameRef=ABRecordCopyValue(人,kABPersonFirstNameProperty);   NSString * lastname=(__bridge_transfer NSString *) (lastNameRef);   NSString * firstname=(__bridge_transfer NSString *) (firstNameRef);   NSString *名称=[NSString stringWithFormat: @ % @ % @, lastname==NULL & # 63;@ ":姓,firstname==NULL & # 63;@ " firstname):;   NSLog(@”姓名:% @”,名称);      ContactsModel *模型=[[ContactsModel alloc] initWithName:名字num: phoneValue);   NSLog(@“电话号码:% @”,phoneValue);      CFRelease (phonesRef);   如果self.myBlock self.myBlock(模型);   }      # pragma马克- ContactsUI   - (void) getContactsFromContactUI:目标(ui *) {   CNContactPickerViewController * pickerVC=[[CNContactPickerViewController alloc] init);   pickerVC.delegate=自我;   (目标presentViewController: pickerVC动画:是的完成:nil);   }      - (void) contactPicker: (CNContactPickerViewController *)选择didSelectContact:接触(CNContact *) {   NSString *名称=[NSString stringWithFormat: @ % @ % @,接触。familyName==NULL & # 63;@ ":接触。familyName,接触。givenName==NULL & # 63;”@“contact.givenName):;   NSLog(@”姓名:% @”,名称);      CNPhoneNumber * phoneNumber=[接触。phonenumber[0]值);   ContactsModel *模型=[[ContactsModel alloc] initWithName:名字num: [NSString stringWithFormat: @ % @, phoneNumber.stringValue]];   NSLog(@“电话号码:% @”,phoneNumber.stringValue);      如果self.myBlock self.myBlock(模型);   }      # pragma马克- AddressBook   + (NSMutableArray *) getContactsFromAddressBook {   ABAuthorizationStatus状态=ABAddressBookGetAuthorizationStatus ();   CFErrorRef myError=零;   ABAddressBookRef addressBook=ABAddressBookCreateWithOptions (NULL,和myError);   如果(myError) {   (自我showErrorAlert);   如果addressBook CFRelease (addressBook);   返回nil;   }      __block NSMutableArray * contactModels=[NSMutableArray数组);   如果(状态==kABAuthorizationStatusNotDetermined){//用户还没有决定是否授权你的程序进行访问   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

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