Search code examples
iphoneobjective-ccocoa-touchios4iphone-sdk-3.0

Unable to add contact in group using ABGroupAddMember in iphone?


I am using the following code but still its not able to add contact information in group and one more thing it always use to create new group. i also want to check that exisiting gruop is avaialble or not !!!!

Unable to add contact in group !!

ABRecordRef group = ABGroupCreate(); //create a group 
ABRecordSetValue(group, kABGroupNameProperty,@"My Group", &error); // set group's name 
ABGroupAddMember(group, person, &error); // add the person to the group 
ABAddressBookAddRecord(addressBook, group, &error); // add the group   
ABAddressBookSave(addressBook, nil); //save the record   

Solution

  • Please find the working code below ...

    ABRecordRef aRecord = ABPersonCreate(); 
        CFErrorRef  anError = NULL; 
        ABRecordSetValue(aRecord, kABPersonFirstNameProperty, 
                         CFSTR("Jijo"), &anError); 
        ABRecordSetValue(aRecord, kABPersonLastNameProperty, 
                         CFSTR("Pulikkottil"), &anError); 
        if (anError != NULL) { 
    
            NSLog(@"error while creating..");
        } 
        CFStringRef firstName, lastName; 
        firstName = ABRecordCopyValue(aRecord, kABPersonFirstNameProperty); 
        lastName  = ABRecordCopyValue(aRecord, kABPersonLastNameProperty); 
    
    
    
    
        ABAddressBookRef addressBook; 
        CFErrorRef error = NULL; 
        addressBook = ABAddressBookCreate(); 
    
        BOOL isAdded = ABAddressBookAddRecord (
                                addressBook,
                                aRecord,
                                 &error
        );
    
        if(isAdded){
    
            NSLog(@"added..");
        }
        if (error != NULL) {
            NSLog(@"ABAddressBookAddRecord %@", error);
        } 
        error = NULL;
    
        BOOL isSaved = ABAddressBookSave (
                           addressBook,
                           &error
        );
    
        if(isSaved){
    
            NSLog(@"saved..");
        }
    
        if (error != NULL) {
            NSLog(@"ABAddressBookSave %@", error);
        } 
    
        CFRelease(aRecord); 
        CFRelease(firstName); 
        CFRelease(lastName); 
        CFRelease(addressBook);
    

    Don't forget add the AddressBook.Framework.

    Ref: AddressBookProgrammingGuideforiPhone.pdf.

    The same is discussed

    http://www.iphonedevsdk.com/forum/iphone-sdk-development/12496-add-contact-address-book.html