IOS package to obtain a list of tools

IOS gets the mail list a total of 4 framework: AddressBook, AddressBookUI, Contacts, ContactsUI; AddressBook and AddressBookUI have been iOS9 deprecated, and introduced Contacts and ContactsUI to replace the AddressBookUI and ContactsUI. The picker is an interface to provide a contact information and does not require manual authorization, AddressBook and Contacts get all the mail list data and the need for manual authorization.

Note that access to address book permissions in the iOS10 need to take the initiative in the info.plist to add prompt information. Otherwise it will crash. Add a pair of key and value in info.plist

  • Key: Privacy – Contacts Usage Description
  • Value: free play, here literally write a sentence: do you want this App to access your address book?

ContactsModel

Create two new data model files to save access to the address book data

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

This is access to the address book of the tool class

ContactsHelp.h

#import < UIKit/UIKit.h> #import "ContactsModel.h" typedef void (^ContactBlock) (ContactsModel *contactsModel); @interface: NSObject + ContactsHelp (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] > @interface = 9) and 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 * didSelectPerson: (peoplePicker) ABRecordRef phonesRef = ABRecordCopyValue person) {ABMultiValueRef (person, kABPersonPhoneProperty); if (! PhonesRef) {return} (*phoneValue = NSString; __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:% @ ", name); ContactsModel *model = [[ContactsModel alloc] initWithName:name num:phoneValue]; NSLog (@ Tel:% @", 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, contact.familyName = = "stringWithFormat:@%@%@" @ NULL? "": contact.familyName, contact.givenName = NULL? @ "": contact.givenName]; NSLog (@ Name:% @ ", name); CNPhoneNumber *phoneNumber = [contact.phoneNumbers[0] value]; ContactsModel *model = [[ContactsModel alloc] initWithName:name num:[NSString stringWithFo Rmat:@ phoneNumber.stringValue]]; NSLog "% @, (@" telephone number:% @ ", phoneNumber.stringValue); if (self.myBlock) self.myBlock (model);} #pragma - AddressBook + mark (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) {/ / the user has not yet decided whether to authorize your program to access ABAddressBookRequ EstAccessWithCompletion (addressBook ^ (bool granted, CFErrorRef, error) {if (granted) {contactModels} else {[self = getAddressBookInfo:addressBook]; [self showErrorAlert]; if (addressBook) CFRelease (addressBook);}}); / / the user has refused or iOS equipment on parental control or other licensing procedures and configuration to prevent mail list a database else if} (status = = kABAuthorizationStatusDenied || status = = kABAuthorizationStatusRestricted) {[self showErrorAlert]; if (addressBook) CFRelease (addressBook);} else if (status = = kABAuthorizationStatusAuthorized) {/ / contactModels = [self getAddress authorized users BookInfo: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 (per) Son, kABPersonFirstNameProperty); NSString *name = [NSString, lastName = = "stringWithFormat:@%@%@" NULL? "@": lastName, firstName = NULL? @ "": firstName]; NSLog (@ Name:% @ ", name); CFIndex phoneCount = ABMultiValueGetCount (phones); for (int j = 0; J < phoneCount; j++) {NSString *phoneValue = (__bridge_transfer * NSString) ABMultiValueCopyValueAtIndex (phones, J); NSLog (@ Tel:% @", 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 - Contacts + mark (NSMutableArray * getContactsFromContacts) {CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]; CNContactStore *store = [[CNContactStore alloc] init]; __block NSMutableArray *contactModels = [NSMutableArray array]; if (status = = CNAuthorizationStatusNotDetermined) {/ / the user has not yet decided whether to authorize your program to access [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^ (BOOL granted, NSError * _Nullable error if (granted)) {{contactModels} else {[self = getContactsInfo:store]; [se LF showErrorAlert];} / /}]; the user has refused or parental control on the iOS device or some other license procedures and prevent mail list database else if} (status = = CNAuthorizationStatusDenied || status = = CNAuthorizationStatusRestricted) {[self showErrorAlert]}; else if (status = = CNAuthorizationStatusAuthorized) {/ / contactModels = [self getContactsInfo:store] authorized users; 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, contact.familyName = = "stringWithFormat:@%@%@" NULL? "@": contact.familyName, contact.givenName = NULL? @ "": contact.givenName]; NSLog (@ "% @ Name:" name (for); CNLabeledValue *labeledValue in contact.phoneNumbers CNPhoneNumber) {*phoneNumber = labeledValue.value; NSLog (@ Tel:% @ ", phoneNumber.stringValue); ContactsModel *model = [[ContactsModel alloc] initWithName:name num:phoneNumber.stringValue]; [contactModels addObject:model];}}]; Return contactModels;} #pragma - Error + mark (void showErrorAlert) {NSLog (@ authorization failure, please allow app to access your mail list, in the mobile phone "Settings - Privacy - mail list" option to allow ");} @end

Use

#import "ContactsHelp.h" #import "ContactsModel.h"... @property (nonatomic, strong) ContactsHelp *contactsHelp;... - (IBAction) btn_getOne [[ContactsHelp alloc] {self.contactsHelp = 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); }];}