I am working on an iPhone application which works with the address book contacts. I am trying to get make groups in the contacts but the problem is when I run the application again the group is created again and the new contact is saved to that new created group.
// create address book record
ABAddressBookRef addressBook = ABAddressBookCreate();
// create a person
ABRecordRef person = ABPersonCreate();
// name of the new person
ABRecordSetValue(person, kABPersonFirstNameProperty, [index objectAtIndex:3], nil);
ABRecordSetValue(person, kABPersonLastNameProperty, [index objectAtIndex:0], nil);
//add the new person to the record
ABAddressBookAddRecord(addressBook, person, nil);
ABAddressBookSave(addressBook, &error);
ABAddressBookAddRecord(addressBook, group, &error); // add the group
ABAddressBookSave(addressBook, &error);
ABRecordRef group = ABGroupCreate(); //create a group
ABGroupAddMember(group, person, &error); // add the person to the group
ABAddressBookSave(addressBook, &error);
//save the record
ABAddressBookSave(addressBook, nil);
// relase the ABRecordRef variable
CFRelease(person);
That's the code I have been working on.