1

I am making an app where I need to save a contact to the address book. Everything works fine except when I add the kABPersonAddressProperty, first I add them then i save the address and it crashes while saving.

The error I am getting is:

-[__NSCFString count]: unrecognized selector sent to instance 0x99e6f30
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString count]: unrecognized selector sent to instance 0x99e6f30'

Here is the code that I am using:

ABRecordRef aRecord = ABPersonCreate();
CFErrorRef  anError = NULL;
//
//some code here, not relevant
//
ABMutableMultiValueRef multiAdd = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.Street.text, kABPersonAddressStreetKey, NULL);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.ZIPcode.text, kABPersonAddressZIPKey, NULL);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.City.text, kABPersonAddressCityKey, NULL);

ABRecordSetValue(aRecord, kABPersonAddressProperty, multiAdd, &anError);
CFRelease(multiAdd);

//More irrelevant code here


ABAddressBookRef addressBook;
CFErrorRef error = NULL;
addressBook = ABAddressBookCreateWithOptions(nil, NULL);

BOOL isAdded = ABAddressBookAddRecord (addressBook, aRecord, &error);

if(isAdded){
    NSLog(@"added..");
}
if (error != NULL) {
    NSLog(@"ABAddressBookAddRecord %@", error);
}

error = NULL;

BOOL isSaved = ABAddressBookSave (addressBook, &error);

Whenever I run this code, the error are always NULL, and isAdded is always true, but still it crashes while executing ABAddressBookSave(addressBook,&error); Another important thing is that if I delete this part of code:

ABMutableMultiValueRef multiAdd = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.Street.text, kABPersonAddressStreetKey, NULL);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.ZIPcode.text, kABPersonAddressZIPKey, NULL);
ABMultiValueAddValueAndLabel(multiAdd, (__bridge CFStringRef)self.City.text, kABPersonAddressCityKey, NULL);

ABRecordSetValue(aRecord, kABPersonAddressProperty, multiAdd, &anError);
CFRelease(multiAdd);

The contact is added fine, with name, last name, multiple phone numbers, URL and email.

Shiva
  • 20,575
  • 14
  • 82
  • 112
Alejandro
  • 23
  • 4

1 Answers1

2

The address property is not a kABMultiStringPropertyType, but instead a kABMultiDictionaryPropertyType.

To resolve the crash try changing your call to ABMultiValueCreateMutable by passing it the right type, kABMultiDictionaryPropertyType.

And then you'll also need to update how you are populating the address information by creating a dictionary out of the address string values. Check out this post for an example on that.

It should look something like this (untested):

ABMultiValueRef addresses = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
   NSDictionary *values = [NSDictionary dictionaryWithObjectsAndKeys:
                               self.Street.text, (NSString *)kABPersonAddressStreetKey,
                               self.ZIPcode.text, (NSString *)kABPersonAddressZIPKey,
                               self.City.text, (NSString *)kABPersonAddressCityKey,
                               nil];

ABMultiValueAddValueAndLabel(addresses, (CFDictionaryRef)values, kABHomeLabel, NULL);
ABRecordSetValue(aRecord, kABPersonAddressProperty, addresses, &anError);
Community
  • 1
  • 1
foggzilla
  • 1,705
  • 11
  • 15