Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Address Book UI Framework deprecated methods

I am working on an existing objective c project , While reading Address Book UI Framework Reference for iOS i found the below classes have deprecated in iOS 9 . ( ABUnknownPersonViewController , ABPersonViewController , ABPeoplePickerNavigationController, ABNewPersonViewController ) What is the replacement of this .? Where i can find some document related this . any help appreciated . Thanks in advance .

like image 252
Sat Avatar asked Jan 29 '16 07:01

Sat


1 Answers

The AddressBookUI framework has been deprecated in iOS 9, so better you should use ContactsUI Framework.

It has many new features including all the features of AddressBookUI framework.

So, in case if you are targeting the iOS 9 specifically then you should go for ContactsUI Framework.

To check that AddressBookUI framework is available for specific iOS version you can do the following:

if ([CNContactStore class]) {
  CNContactStore *store = [CNContactStore new];
  //...
} else {
  // Fallback to old framework
}

Here is the complete code for that:

- (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 that you ask the permission to read the contacts from device.

Reference link: https://gist.github.com/willthink/024f1394474e70904728

Updated:

For replacement for AddressBookUI you need to use CNContactPickerViewController. You can check the delegate methods which can be used to pickup the one or multiple contacts at a time.

This will present a inbuilt UIViewController with all the contacts and you need to implement the delegate methods of it!

To select one contact:

contactPicker:didSelectContact:

To select multiple (New Feature):

contactPicker:didSelectContacts:

CNContactPickerDelegate reference: https://developer.apple.com/library/ios/documentation/ContactsUI/Reference/CNContactPickerDelegate_Protocol/

like image 184
Sohil R. Memon Avatar answered Sep 28 '22 11:09

Sohil R. Memon