The AddressBookUI framework is deprecated in iOS 9 , so itβs best to use the ContactsUI Framework.
It has many new features, including all the functions of the AddressBookUI structure.
So, if you are targeting iOS 9 , then you should go to ContactsUI Framework.
To verify that the AddressBookUI structure is available for a specific version of iOS , you can do the following:
if ([CNContactStore class]) { CNContactStore *store = [CNContactStore new];
Here is the complete code for this:
- (void) contactScan { if ([CNContactStore class]) { //ios9 or later CNEntityType entityType = CNEntityTypeContacts; if( [CNContactStore authorizationStatusForEntityType:entityType] == CNAuthorizationStatusNotDetermined) { CNContactStore * contactStore = [[CNContactStore alloc] init]; [contactStore requestAccessForEntityType:entityType completionHandler:^(BOOL granted, NSError * _Nullable error) { if(granted){ [self getAllContact]; } }]; } else if( [CNContactStore authorizationStatusForEntityType:entityType]== CNAuthorizationStatusAuthorized) { [self getAllContact]; } } } -(void)getAllContact { if([CNContactStore class]) { //iOS 9 or later NSError* contactError; CNContactStore* addressBook = [[CNContactStore alloc]init]; [addressBook containersMatchingPredicate:[CNContainer predicateForContainersWithIdentifiers: @[addressBook.defaultContainerIdentifier]] error:&contactError]; NSArray * keysToFetch =@ [CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPostalAddressesKey]; CNContactFetchRequest * request = [[CNContactFetchRequest alloc]initWithKeysToFetch:keysToFetch]; BOOL success = [addressBook enumerateContactsWithFetchRequest:request error:&contactError usingBlock:^(CNContact * __nonnull contact, BOOL * __nonnull stop){ [self parseContactWithContact:contact]; }]; } } - (void)parseContactWithContact :(CNContact* )contact { NSString * firstName = contact.givenName; NSString * lastName = contact.familyName; NSString * phone = [[contact.phoneNumbers valueForKey:@"value"] valueForKey:@"digits"]; NSStrubg * email = [contact.emailAddresses valueForKey:@"value"]; NSArray * addrArr = [self parseAddressWithContac:contact]; } - (NSMutableArray *)parseAddressWithContac: (CNContact *)contact { NSMutableArray * addrArr = [[NSMutableArray alloc]init]; CNPostalAddressFormatter * formatter = [[CNPostalAddressFormatter alloc]init]; NSArray * addresses = (NSArray*)[contact.postalAddresses valueForKey:@"value"]; if (addresses.count > 0) { for (CNPostalAddress* address in addresses) { [addrArr addObject:[formatter stringFromPostalAddress:address]]; } } return addrArr; }
Just make sure you request permission to read contacts from the device.
Link Link: https://gist.github.com/willthink/024f1394474e70904728
Updated:
To replace AddressBookUI you need to use CNContactPickerViewController . You can check delegation methods that can be used to select one or more contacts at once.
This will contain a built-in UIViewController with all contacts, and you need to implement its delegate methods!
To select one contact:
contactPicker:didSelectContact:
To select multiple (new feature):
contactPicker:didSelectContacts:
CNContactPickerDelegate link: https://developer.apple.com/library/ios/documentation/ContactsUI/Reference/CNContactPickerDelegate_Protocol/