I’m having real trouble with the AddressBook and memory. I want it to return a record (either the existing one or a new one) that I can save a contact into. There is a lot wrong with this function according to the build and analyze tool. I have read the documentation and re-written this a bunch of times but I cant seem to get it right.
Whats the correct way to write this method? / What am I doing wrong?
//Searches Groups by name for matches and returns the attached record
- (ABRecordRef)getGroup
{
ABRecordRef response;
NSArray *groups = (NSArray*)ABAddressBookCopyArrayOfAllGroups(addressBook);
for (id group in groups)
{
NSString *currentGroup = (NSString*)ABRecordCopyValue(group, kABGroupNameProperty);
if ([currentGroup isEqualToString:GroupName])
{
response = group;
CFRelease(currentGroup); //??
break;
}
CFRelease(currentGroup); //?
}
[groups release];
if (response == NULL)
{
response = ABGroupCreate();
ABRecordSetValue(response, kABGroupNameProperty, GroupName, &error);
ABAddressBookAddRecord(addressBook, response, &error);
ABAddressBookSave(addressBook, &error);
}
return response;
}
Its easier and more reliable to just get the Record ID.
If found this on an unrelated post – https://stackoverflow.com/a/8249975/874927