Hi everybody, I'm trying to create a
new contact and add it to the
AddressBook but when I get to the
ABAddressSave line of code I get
EXC_BAD_ACCESS. I cannot see what am I
doing wrong, I enabled NSZombie to
check if this is a memory related
error but it didn't spot any. Can
anybody tell me what is wrong with
this code? Thank you in advance!
CFErrorRef error = NULL;
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
ABRecordRef newRecord = ABPersonCreate();
ABRecordSetValue(newRecord, kABPersonFirstNameProperty, @"Xxxxxx", &error);
ABRecordSetValue(newRecord, kABPersonURLProperty, @"Yyyyyy", &error);
//Add phone numbers to record
ABMutableMultiValueRef phones = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(phones, @"1-555-555-5555", kABWorkLabel, NULL);
ABRecordSetValue(newRecord, kABPersonPhoneProperty, phones, &error);
CFRelease(phones);
//Add email address to record
ABMutableMultiValueRef emails = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(emails, @"
[email protected]", kABWorkLabel, NULL);
ABRecordSetValue(newRecord, kABPersonEmailProperty, emails, &error);
CFRelease(emails);
ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDict = [[NSMutableDictionary alloc] init];
[addressDict setObject:@"xxx1" forKey:(NSString *)kABPersonAddressStreetKey];
[addressDict setObject:@"xxx2" forKey:(NSString *)kABPersonAddressCityKey];
[addressDict setObject:@"xxx3" forKey:(NSString *)kABPersonAddressStateKey];
[addressDict setObject:@"xxx4" forKey:(NSString *)kABPersonAddressZIPKey];
ABMultiValueAddValueAndLabel(multiAddress, addressDict, kABWorkLabel, NULL);
ABRecordSetValue(newRecord, kABPersonAddressProperty, multiAddress, &error);
CFRelease(multiAddress);
[addressDict release];
ABAddressBookAddRecord(iPhoneAddressBook, newRecord, &error);
ABAddressBookSave(iPhoneAddressBook, NULL);
if(error != nil){
NSLog(@"Error creating contact:%@", error);
}