Address Book UI Framework deprecated methods
Asked Answered
E

2

6

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 .

Esquivel answered 29/1, 2016 at 7:11 Comment(0)
E
11

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/

Elongation answered 29/1, 2016 at 7:15 Comment(9)
I am assuming ContactsUI won't be available for <9.0 iOS versions? Let's suppose I am targeting iOS 7 and above. How do you suppose I should tackle it? (Since I am getting deprecated warning for methods using AddressBook Framework\Alviani
Yes, you should tackle that method using the above code if ([CNContactStore class]) { CNContactStore *store = [CNContactStore new]; //... } else { // Fallback to old framework }Elongation
But falling back to old method will still retain the deprecated warning in my project. What's the incentive of moving to ContactsUI then?Alviani
The advantage is AddressBookUI is unavailable in iOS 9, so there you must be use ContactsUI and if you running on earlier version then you must be using AddressBookUI there ContactsUI will not be available. P.S. The deprecated warning will be there!Elongation
Not really. Even though it is deprecated, AddressBookUI is still working for iOS 9 in my App. Thank you anyways.Alviani
Yes it will be! But as the method are deprecated the Apple can stop support for it! So, the only option is to use the ContactsUIElongation
I believe that if you specify a deployment target of less than iOS 9.0, Xcode will stop warning you about the deprecation? Happens for other deprecated methods/classes etc.Apologize
@Rob I have updated the answer. Thanks for spotting me out!Elongation
How about the view for creating a new contact? Should I use this? init(forUnknownContact contact: CNContact) and then the user can add the contact? The problem it's I have 2 steps before creating a contact.Polydactyl
P
0

Apple has introduced new framework for this for iOS9 and above please fine below link Link

Edit: one more link :Link2

Phanotron answered 29/1, 2016 at 7:14 Comment(1)
Please avoid link-only answers. At the very least you should mention the name of the new framework (in this case, ContactsUI) so that if the links break in the future, someone reading the answer can at least search it by name.Apologize

© 2022 - 2024 — McMap. All rights reserved.