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-ContactsUsageDescription
- value:自由发挥,这里随便写一句:是否允许此App访问你的通讯录?
ContactsModel
新建两个数据模型文件来保存获取的通讯录数据
ContactsModel.h
#import@interfaceContactsModel:NSObject @property(nonatomic,copy)NSString*num; @property(nonatomic,copy)NSString*name; -(instancetype)initWithName:(NSString*)namenum:(NSString*)num; @end
ContactsModel.m
#import"ContactsModel.h" @implementationContactsModel -(instancetype)initWithName:(NSString*)namenum:(NSString*)num{ if(self=[superinit]){ self.name=name; self.num=num; } returnself; } @end
ContactsHelp
这是获取通讯录的工具类.
ContactsHelp.h
#import#import"ContactsModel.h" typedefvoid(^ContactBlock)(ContactsModel*contactsModel); @interfaceContactsHelp:NSObject +(NSMutableArray*)getAllPhoneInfo; -(void)getOnePhoneInfoWithUI:(UIViewController*)targetcallBack:(ContactBlock)block; @end
ContactsHelp.m
#import"ContactsHelp.h" #import#import #import #import #defineiOS9([[[UIDevicecurrentDevice]systemVersion]floatValue]>=9.0) @interfaceContactsHelp() @property(nonatomic,strong)ContactsModel*contactModel; @property(nonatomic,strong)ContactBlockmyBlock; @end @implementationContactsHelp +(NSMutableArray*)getAllPhoneInfo{ returniOS9?[selfgetContactsFromContacts]:[selfgetContactsFromAddressBook]; } -(void)getOnePhoneInfoWithUI:(UIViewController*)targetcallBack:(void(^)(ContactsModel*))block{ if(iOS9){ [selfgetContactsFromContactUI:target]; }else{ [selfgetContactsFromAddressBookUI:target]; } self.myBlock=block; } #pragmamark-AddressBookUI -(void)getContactsFromAddressBookUI:(UIViewController*)target{ ABPeoplePickerNavigationController*pickerVC=[[ABPeoplePickerNavigationControlleralloc]init]; pickerVC.peoplePickerDelegate=self; [targetpresentViewController:pickerVCanimated:YEScompletion:nil]; } -(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePickerdidSelectPerson:(ABRecordRef)person{ ABMultiValueRefphonesRef=ABRecordCopyValue(person,kABPersonPhoneProperty); if(!phonesRef){return;} NSString*phoneValue=(__bridge_transferNSString*)ABMultiValueCopyValueAtIndex(phonesRef,0); CFStringReflastNameRef=ABRecordCopyValue(person,kABPersonLastNameProperty); CFStringReffirstNameRef=ABRecordCopyValue(person,kABPersonFirstNameProperty); NSString*lastname=(__bridge_transferNSString*)(lastNameRef); NSString*firstname=(__bridge_transferNSString*)(firstNameRef); NSString*name=[NSStringstringWithFormat:@"%@%@",lastname==NULL?@"":lastname,firstname==NULL?@"":firstname]; NSLog(@"姓名:%@",name); ContactsModel*model=[[ContactsModelalloc]initWithName:namenum:phoneValue]; NSLog(@"电话号码:%@",phoneValue); CFRelease(phonesRef); if(self.myBlock)self.myBlock(model); } #pragmamark-ContactsUI -(void)getContactsFromContactUI:(UIViewController*)target{ CNContactPickerViewController*pickerVC=[[CNContactPickerViewControlleralloc]init]; pickerVC.delegate=self; [targetpresentViewController:pickerVCanimated:YEScompletion:nil]; } -(void)contactPicker:(CNContactPickerViewController*)pickerdidSelectContact:(CNContact*)contact{ NSString*name=[NSStringstringWithFormat:@"%@%@",contact.familyName==NULL?@"":contact.familyName,contact.givenName==NULL?@"":contact.givenName]; NSLog(@"姓名:%@",name); CNPhoneNumber*phoneNumber=[contact.phoneNumbers[0]value]; ContactsModel*model=[[ContactsModelalloc]initWithName:namenum:[NSStringstringWithFormat:@"%@",phoneNumber.stringValue]]; NSLog(@"电话号码:%@",phoneNumber.stringValue); if(self.myBlock)self.myBlock(model); } #pragmamark-AddressBook +(NSMutableArray*)getContactsFromAddressBook{ ABAuthorizationStatusstatus=ABAddressBookGetAuthorizationStatus(); CFErrorRefmyError=NULL; ABAddressBookRefaddressBook=ABAddressBookCreateWithOptions(NULL,&myError); if(myError){ [selfshowErrorAlert]; if(addressBook)CFRelease(addressBook); returnnil; } __blockNSMutableArray*contactModels=[NSMutableArrayarray]; if(status==kABAuthorizationStatusNotDetermined){//用户还没有决定是否授权你的程序进行访问 ABAddressBookRequestAccessWithCompletion(addressBook,^(boolgranted,CFErrorReferror){ if(granted){ contactModels=[selfgetAddressBookInfo:addressBook]; }else{ [selfshowErrorAlert]; if(addressBook)CFRelease(addressBook); } }); //用户已拒绝或iOS设备上的家长控制或其它一些许可配置阻止程序与通讯录数据库进行交互 }elseif(status==kABAuthorizationStatusDenied||status==kABAuthorizationStatusRestricted){ [selfshowErrorAlert]; if(addressBook)CFRelease(addressBook); }elseif(status==kABAuthorizationStatusAuthorized){//用户已授权 contactModels=[selfgetAddressBookInfo:addressBook]; } returncontactModels; } +(NSMutableArray*)getAddressBookInfo:(ABAddressBookRef)addressBook{ CFArrayRefpeopleArray=ABAddressBookCopyArrayOfAllPeople(addressBook); NSIntegerpeopleCount=CFArrayGetCount(peopleArray); NSMutableArray*contactModels=[NSMutableArrayarray]; for(inti=0;i 使用
#import"ContactsHelp.h" #import"ContactsModel.h" ... @property(nonatomic,strong)ContactsHelp*contactsHelp; ... -(IBAction)btn_getOne{ self.contactsHelp=[[ContactsHelpalloc]init]; [self.contactsHelpgetOnePhoneInfoWithUI:selfcallBack:^(ContactsModel*contactModel){ NSLog(@"-----------"); NSLog(@"%@",contactModel.name); NSLog(@"%@",contactModel.num); }]; } -(IBAction)btn_getAll{ NSMutableArray*contactModels=[ContactsHelpgetAllPhoneInfo]; [contactModelsenumerateObjectsUsingBlock:^(id_Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop){ ContactsModel*model=obj; NSLog(@"-----------"); NSLog(@"%@",model.name); NSLog(@"%@",model.num); }]; }总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。