iOS实现封装一个获取通讯录的工具类详解
程序员文章站
2023-12-20 11:46:34
前言
本文给大家介绍了关于ios如何封装一个获取通讯录工具类的相关内容,ios获取通讯录一共有4个framework: addressbook, addressbooku...
前言
本文给大家介绍了关于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); }]; }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。