Search code examples
iosobjective-cios8addressbookabaddressbook

iOS 8 adding contact to address book


I'm trying to add a contact to the address book in iOS8. Unable to do so anymore. Here's my code below:

 -(void)addPersonToAddressBook {


NSString * fullName = integrationDictionary[@"fullName"];

ABPeoplePickerNavigationController *pp =[ABPeoplePickerNavigationController new];
ABAddressBookRef addressBook = [pp addressBook];
ABRecordRef entry = ABPersonCreate();
CFErrorRef cfError=nil;

ABRecordSetValue(entry, kABPersonFirstNameProperty, (__bridge CFTypeRef)(fullName) , nil);

ABAddressBookAddRecord(addressBook, entry, &cfError);

if (ABAddressBookSave(addressBook, &cfError)) {
    NSString *saveMessage = [NSString stringWithFormat:@"%@ has been added to your address book.", fullName];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Contact Added" message:saveMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
} else {
    NSString *saveMessage = [NSString stringWithFormat:@"There was an error adding %@ to your address book.", fullName];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Uh Oh" message:saveMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

NSLog(@"error is %@", cfError);

The error is showing up as null. Has anyone seen this before? Any workarounds?


Solution

  • The error is returning NULL because there's no error registered.

    The problem is that [pp addressBook] is returning nil. So your ABAddressBookRef addressBook reference is nil.

    The workaround is to use ABAddressBookCreateWithOptions instead of [pp addressBook] method of ABPeoplePickerNavigationController.

    Here's a sample which works just fine on both iOS 7.1 & iOS 8.1:

    -(void)requestAuthorizationAndAddPersonToAddressBook 
    {
    // Request authorization to Address Book
        ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
    
        if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
            ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
                // First time access has been granted, add the contact
                [self addPersonToAddressBook];
            });
        }
        else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
            // The user has previously given access, add the contact
            [self addPersonToAddressBook];
        }
        else {
            // The user has previously denied access
            // Send an alert telling user to change privacy setting in settings app
        }
    }
    
    -(void)addPersonToAddressBook {
    
    
        NSString * fullName = @"James Bond";
    
        CFErrorRef abCreateError = nil;
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &abCreateError);
    
        if (abCreateError) {
            NSLog(@"Error occurred: %@", abCreateError);
        }
    
        ABRecordRef entry = ABPersonCreate();
        CFErrorRef cfError=nil;
    
        ABRecordSetValue(entry, kABPersonFirstNameProperty, (__bridge CFTypeRef)(fullName) , nil);
    
        ABAddressBookAddRecord(addressBook, entry, &cfError);
    
        if (ABAddressBookSave(addressBook, &cfError)) {
            NSString *saveMessage = [NSString stringWithFormat:@"%@ has been added to your address book.", fullName];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Contact Added" message:saveMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        } else {
            NSString *saveMessage = [NSString stringWithFormat:@"There was an error adding %@ to your address book.", fullName];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Uh Oh" message:saveMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
    
             if (cfError) {
                  NSLog(@"error is %@", cfError);
            }
        }