views:

74

answers:

1

I have some lines of Objective-C code like:

ABAddressBookRef addressBook;
CFErrorRef error = NULL;
addressBook = ABAddressBookCreate();
ABRecordRef group = ABGroupCreate();
ABRecordSetValue(group, kABGroupNameProperty,@"My Group", &error);

ABRecordRef person = ABPersonCreate();
ABRecordSetValue(person,kABPersonFirstNameProperty,@"Huy 1111111",&error);
ABRecordSetValue(person,kABPersonLastNameProperty,@"AseDra",&error);
ABGroupAddMember(group, person, &error);

ABAddressBookAddRecord(addressBook, group, &error);

ABAddressBookSave(addressBook,&error);

It run fine, no error, group added to Simulator Address Book but person didn't add to Simunator Address Book. Anybody can tell me where i'm wrong?

A: 

According to the Address Book Programming Guide for iOS (link below), the person record must exist in the address book before you can add them to a group.

"Before a person record can be added to a group, it must already be saved to the Address Book database."

http://developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/500-DirectInteraction/DirectInteraction.html%23//apple_ref/doc/uid/TP40007744-CH6-SW4

So maybe this would work

ABAddressBookRef addressBook;
CFErrorRef error = NULL;
addressBook = ABAddressBookCreate();

ABRecordRef person = ABPersonCreate();
ABRecordSetValue(person,kABPersonFirstNameProperty,@"Huy 1111111",&error);
ABRecordSetValue(person,kABPersonLastNameProperty,@"AseDra",&error);

// add the person record
ABAddressBookAddRecord(addressBook, person, &error);

ABRecordRef group = ABGroupCreate();
ABRecordSetValue(group, kABGroupNameProperty,@"My Group", &error);
ABGroupAddMember(group, person, &error);
ABAddressBookAddRecord(addressBook, group, &error);

ABAddressBookSave(addressBook,&error);
Jake