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);
}
}
}