ABAddressBookCreate deprecated
Asked Answered
F

2

12

I'm creating an application that will save a contact to the address book, but when I use ABAddressBookCreate to my code, it appear as a warning. I'm coding in iOS 7.

My question is, what is the substitute for ABAddressBookCreate in iOS 7.

Any help will be appreciated. Thanks.

Fullback answered 2/4, 2014 at 2:52 Comment(0)
I
35

Use ABAddressBookCreateWithOptions() instead.

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

More info on usage in the documentation here

edit

The AddressBook framework is deprecated in iOS 9 and replaced with the new and improved Contacts framework: https://developer.apple.com/library/ios/documentation/Contacts/Reference/Contacts_Framework/index.html#//apple_ref/doc/uid/TP40015328

Iiette answered 2/4, 2014 at 3:28 Comment(2)
ABAddressBookCreateWithOptions() is also marked deprecated in document.Prune
The AddressBook framework is deprecated in iOS 9 and replaced with the new and improved Contacts framework: developer.apple.com/library/prerelease/ios/documentation/…Iiette
M
8

Contact Frame work in iOS9: Can you plz see the following code

    -(void)contactsFromAddressBook{
//ios 9+
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (granted == YES) {
        //keys with fetching properties
        NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey];
        NSString *containerId = store.defaultContainerIdentifier;
        NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
        NSError *error;
        NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
        if (error) {
            NSLog(@"error fetching contacts %@", error);
        } else {
            NSString *phone;
            NSString *fullName;
            NSString *firstName;
            NSString *lastName;
            UIImage *profileImage;
            NSMutableArray *contactNumbersArray;
            for (CNContact *contact in cnContacts) {
                // copy data to my custom Contacts class.
                firstName = contact.givenName;
                lastName = contact.familyName;
                if (lastName == nil) {
                    fullName=[NSString stringWithFormat:@"%@",firstName];
                }else if (firstName == nil){
                    fullName=[NSString stringWithFormat:@"%@",lastName];
                }
                else{
                    fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName];
                }
                UIImage *image = [UIImage imageWithData:contact.imageData];
                if (image != nil) {
                    profileImage = image;
                }else{
                    profileImage = [UIImage imageNamed:@"person-icon.png"];
                }
                for (CNLabeledValue *label in contact.phoneNumbers) {
                  phone = [label.value stringValue];
                    if ([phone length] > 0) {
                       [contactNumbersArray addObject:phone];
                    }
                }
                NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: fullName,@"fullName",profileImage,@"userImage",phone,@"PhoneNumbers", nil];
               [self.contactsArray addObject:personDict];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableViewRef reloadData];
            });
        }
    }
}];
}
Mattah answered 10/12, 2015 at 4:3 Comment(2)
What if i've to build app for iPhone 4?Indictment
@ Jamil 7, You should use AddressBook Framework. Because software updations stopped for this devices from APPLE. The maximum version is iOS 8 only. Contact Framework use only from in iOS9.Mattah

© 2022 - 2024 — McMap. All rights reserved.