I developed an app based on iOS 4.1, which manages the address book. However, since I have updated to iOS 4.2.1, I can't add a person to a group. However, it is still possible to add a group or a person to the address book.
XCode and SDK are up to date. There are apps in iTunes, which have exactly the same problem after the update, although most of them work still properly.
As I said, it worked with iOS 4.0 and iOS 4.1! Here is an example:
Code:
ABAddressBookRef book = ABAddressBookCreate();
ABRecordRef group = ABGroupCreate();
ABRecordRef person = ABPersonCreate();
ABRecordSetValue(group, kABGroupNameProperty, @"NewGroup", nil);
ABRecordSetValue(person, kABPersonFirstNameProperty, @"Firstname", nil);
ABAddressBookAddRecord(book, person, nil);
ABAddressBookSave(book, nil);
ABGroupAddMember(group, person, nil); //not working since update to iOS 4.2
ABAddressBookAddRecord(book, group, nil);
ABAddressBookSave(book, nil);
Does anyone have the same problem, or have any idea what's wrong here? For answers, I would be very grateful!
According to the docs, "Before a person record can be added to a group, the record must be saved to the Address Book database."
This is actually not super-clear. But I copied the code from the OP's post into a sample project, saw it wasn't working for me either (on 4.2), and then decided that maybe the docs meant that the GROUP record had to be saved.
So I tried this slight modification, and it worked.
Code:
ABAddressBookAddRecord(book, group, nil); // add the group record first!!
ABAddressBookSave(book, nil); // also save the group record first!!
ABGroupAddMember(group, person, nil); // this works now!!
ABAddressBookSave(book, nil);
BTW, first thing I tried was passing in a CFErrorRef to see what the actual error was. Unfortunately ABGroupAddMember doesn't seem to care if you pass one in or not; it never got set to anything even when it was failing.
__________________ Recall It!Tag your notes. Tag your photos. Tag your thoughts. Tag your life.
Hi, a have the same problem when i updated from iOS 4.1 to 4.2.1. ABGroupAddMember always returns false on iOS 4.2.1, but on iOS 4.1 works. The Apple`s technical support said that it's a bug on iOS 4.2. I tried the code that you put here, but i didn' have success. Do you have another solution for this issue? I need solve this asap because I am almost finishing my app.
Thanks a lot!
Quote:
Originally Posted by dljeffery
According to the docs, "Before a person record can be added to a group, the record must be saved to the Address Book database."
This is actually not super-clear. But I copied the code from the OP's post into a sample project, saw it wasn't working for me either (on 4.2), and then decided that maybe the docs meant that the GROUP record had to be saved.
So I tried this slight modification, and it worked.
Code:
ABAddressBookAddRecord(book, group, nil); // add the group record first!!
ABAddressBookSave(book, nil); // also save the group record first!!
ABGroupAddMember(group, person, nil); // this works now!!
ABAddressBookSave(book, nil);
BTW, first thing I tried was passing in a CFErrorRef to see what the actual error was. Unfortunately ABGroupAddMember doesn't seem to care if you pass one in or not; it never got set to anything even when it was failing.
I noticed the same instruction dljeffery pointed out in the docs, however I interpreted it differently, to mean that the person's record must be saved. I modified my code accordingly and everything worked again. Here is a concrete example:
Code:
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef testPerson = ABPersonCreate();
ABRecordSetValue(testPerson, kABPersonFirstNameProperty, @"test", NULL);
/*** WITHOUT THESE 2 LINES THE CALL BELOW WILL FAIL ***/
ABAddressBookAddRecord(addressBook, testPerson, NULL);
ABAddressBookSave(addressBook, NULL);
BOOL success = ABGroupAddMember(groupRec, testPerson, NULL);
this is assuming groupRec is an ABRecordRef pointing to a valid group. success will be true with the call to ABAddressBookAddRecord and ABAddressBookSave, but false without them. hope this helps