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.