IOS 8 adding a contact to the address book

I am trying to add a contact to an address book in iOS8. Couldn't do it 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 displayed as null. Has anyone seen this before? Any workarounds?

+3


source to share


1 answer


The error returns NULL

because an error was logged.

The problem is what it [pp addressBook]

returns nil

. So your link ABAddressBookRef addressBook

nil

.

A workaround is to use ABAddressBookCreateWithOptions

instead of a [pp addressBook]

method ABPeoplePickerNavigationController

.



Here's a sample that works great on both iOS 7.1 and 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);
        }
    }

      

+2


source







All Articles