1

I am writing my first iPhone app. I am trying to add a new contact in my code, when I do so, I get the following error: Error Domain=ABAddressBookErrorDomain Code=1 "The operation couldn’t be completed. (ABAddressBookErrorDomain error 1.)".

I searched around and it seems to be something about the app may not have access to the Contacts apps, but I thought I already checked and coded to request for access if access is denied. Can someone help out pls?

Here is my code:

    CFErrorRef err = nil;
    ABAuthorizationStatus stat = ABAddressBookGetAuthorizationStatus();
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    ABRecordRef person = ABPersonCreate();
    ABMutableMultiValueRef addr = ABMultiValueCreateMutable(kABMultiStringPropertyType);

    if (addressBook == nil) {
        NSLog(@"error: %@", err);
    }
    else{
        if (stat==kABAuthorizationStatusDenied || stat==kABAuthorizationStatusRestricted) {
            NSLog(@"%@", @"no access.  Please change privacy settings under Privacy > Settings");
        }
        else{
            NSLog(@"Starting Contact Build...");
            ABRecordSetValue(person, kABPersonFirstNameProperty, @"Jimmy", &err);
            ABRecordSetValue(person, kABPersonLastNameProperty, @"Johns", &err);

            ABMultiValueAddValueAndLabel(addr, @"1-123-456-7890", kABPersonPhoneIPhoneLabel, NULL);

            ABMultiValueAddValueAndLabel(addr, @"jimmy@corpmail.com", kABHomeLabel, nil);
            ABRecordSetValue(person, kABPersonEmailProperty, addr, nil);

            ABAddressBookAddRecord(addressBook, person, &err);
            ABAddressBookSave(addressBook, &err);
            if (addr) CFRelease(addr);
            if (person) CFRelease(person);


            if (err != NULL)
            {

                NSLog(@"Some error... %@", err);

            }
        }
    }
Lavvo
  • 1,024
  • 3
  • 16
  • 35
  • Are you sure your app is authorized to access contacts? – pNre Apr 05 '14 at 19:28
  • 1
    Test this kind of code _only on a device_. It won't work in the Simulator. – matt Apr 05 '14 at 19:39
  • I just found another piece of code that can be used to validate authorization is indeed active, I tried that code and that seems to force the popup that asks a user for Authorization. Once that worked, I wrapped my code that saves contacts into a new function and everything seems to be working fine now. Thanks for all your help. – Lavvo Apr 05 '14 at 19:43
  • 1
    @matt the Adress book does work on the simulator – erdekhayser Apr 11 '14 at 19:36

1 Answers1

0

I had same issues without access permission. i had solved with help of used below link

The Answer was posted by Kyle here: https://stackoverflow.com/a/12648938/480415

Community
  • 1
  • 1
codercat
  • 22,873
  • 9
  • 61
  • 85