Search code examples
iphoneaddressbook

addressbook implementation in iphone


In my application,i need to create groups of contacts for sending group sms.First the groips are created and the members are then added.The issue is the newly added contact is added twice to the all contacts list and once to that particular group.I need to save that contact to the group and all contact lists only once.What is wrong with my code?I am using the following code.

- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
{}

when this method is called,the newly entered contact is added to the all contacts list. Then creating an addressbook instance addressBook = ABAddressBookCreate();The person i need to save to the group is the contact added b the user from the interface.So the person is set to displayed person.

displayedPerson=person;
person = ABPersonCreate();
CFErrorRef error = NULL;

CFStringRef firstName, lastName; 
firstName = ABRecordCopyValue(displayedPerson, kABPersonFirstNameProperty); 
lastName  = ABRecordCopyValue(displayedPerson, kABPersonLastNameProperty);  
NSString *fname=(NSString *)firstName;
NSString *lname=(NSString *)lastName;


ABRecordSetValue(person, kABPersonFirstNameProperty,fname, &error);
ABRecordSetValue(person, kABPersonLastNameProperty,lname, &error);

ABAddressBookAddRecord(addressBook, person, &error);
ABAddressBookSave(addressBook, &error);

parentGroup = ABAddressBookGetGroupWithRecordID(addressBook,groupID);
ABAddressBookAddRecord(addressBook,  parentGroup, &error); 
ABAddressBookSave(addressBook, &error);

ABGroupAddMember(parentGroup, person, &error);
ABAddressBookSave(addressBook, nil);`
CFRelease(person);

But after doing this the contact is added again to the all contacts list.Can anyone help?


Solution

  •   - (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
        {
            CFErrorRef error=NULL;
            ABRecordRef parentGroup = ABAddressBookGetGroupWithRecordID(newPersonViewController.addressBook,appdelegate.default_Group);//set selected group id
            ABGroupAddMember(parentGroup, person,&error);
            ABAddressBookSave(newPersonViewController.addressBook, &error);
            ABRecordID rcId = ABRecordGetRecordID(person);
             ABAddressBookSave (newPersonViewController.addressBook, &error);
    
            [newPersonViewController dismissModalViewControllerAnimated:YES];
        }