1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
alanvabraham
  • 779
  • 2
  • 14
  • 25

2 Answers2

1

ABRecordRef group = ABGroupCreate(); ,

It will create a new group... If you wan to add member in existing group then you should get group by its id. Updated Code

 ABRecordRef group = ABAddressBookGetGroupWithRecordID(addressBookInstance,putYourGroupIdHere);

Thanks,

Ravin
  • 8,544
  • 3
  • 20
  • 19
  • i have tried what you have mentioned but i am getting an error like "Initialization makes integer from pointer without a cast" – alanvabraham Apr 21 '11 at 06:51
  • sorry for troubling you but it was my mistake. I written method incorrectly please see updated code in above answer. Thanks – Ravin Apr 21 '11 at 07:25
  • @Ravin Can you help me with question: [http://stackoverflow.com/questions/10333810/add-contact-to-existing-group-programatically-in-iphone][1] – iOSAppDev Apr 27 '12 at 08:52
-1
ABAddressBookRef ab = ABAddressBookCreate();
CFErrorRef error;
MySource *source = [sourcesAndGroups objectAtIndex:0];
ABRecordRef group = [source.groups objectAtIndex:self.IndexValue]; //Get the Group Name

NSString *firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName  = ABRecordCopyValue(person, kABPersonLastNameProperty);

ABRecordSetValue(group, kABGroupNameProperty,[self nameForGroup:group], &error);
ABAddressBookAddRecord(ab, group, &error);
ABAddressBookSave(ab, &error);
//Create new Person and save to this group
ABRecordRef record = ABPersonCreate();
BOOL isSuccess ;

isSuccess  = ABRecordSetValue(record, kABPersonNicknameProperty,lastName, &error);
isSuccess = ABRecordSetValue(record, kABPersonMiddleNameProperty, firstName , &error);

ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
if(ABMultiValueGetCount(phoneNumbers) == 0)
{
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Phone Number" message:@"Please enter Phone number" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [av show];
    return;
}
CFRelease(phoneNumbers);
NSString* phoneNumber = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);    

ABMutableMultiValueRef multi = ABRecordCopyValue(record, kABPersonEmailProperty);
ABMutableMultiValueRef copyOfPhones = ABMultiValueCreateMutable(kABPersonPhoneProperty);


ABMultiValueAddValueAndLabel(copyOfPhones, phoneNumber,kABPersonPhoneMobileLabel,NULL);
isSuccess = ABRecordSetValue(record, kABPersonPhoneProperty, copyOfPhones, &error);
isSuccess = ABRecordSetValue(record, kABPersonEmailProperty, multi, &error);

isSuccess = ABAddressBookAddRecord(ab, record, &error);
isSuccess = ABAddressBookSave(ab, &error);

ABGroupAddMember(group, record, &error);

NSLog(@"is success %d", isSuccess);

ABAddressBookSave(ab, &error);
CFRelease(group);
Ankit Vyas
  • 7,507
  • 13
  • 56
  • 89
  • I've downvoted this, as without any description, readers probably won't learn anything from it. If you can add some explanation that would be great, but otherwise it may be worth deleting (the question was poor anyway). – halfer Apr 20 '20 at 20:53