开发者

Unable to add contact in group using ABGroupAddMember in iphone?

开发者 https://www.devze.com 2023-02-18 02:49 出处:网络
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

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 !!!!

Un开发者_StackOverflow社区able 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   


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


I have used following way to achieve this task.

1. Get group unique ID.

ABRecordRef currentGroup = (ABRecordRef)CFBridgingRetain([source.groups objectAtIndex:groupIndex]);  
ABRecordID currentGroupID=ABRecordGetRecordID(currentGroup);

2. Add member to group.

ABRecordRef currentGroup = ABAddressBookGetGroupWithRecordID(addressBook, currentGroupID);  
BOOL didAdd,didSave;
NSString *strPersonContactID=[appDelegate.arrOfSelectedContactsToEdit objectAtIndex:i];
ABRecordID personContactID=(ABRecordID)[strPersonContactID intValue];
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, personContactID);
didAdd = ABGroupAddMember(currentGroup,person,&error);
if (!didAdd)       
 {
 NSLog(@"Unresolved error while adding person group");

 }

 didSave = ABAddressBookSave(addressBook, &error);

if (!didSave)      
  {
NSLog(@"Unresolved error while saving address book");
 }

 CFRelease(addressBook);

And if you want to check the group existence, use the group ID, this will help you to differentiate the groups uniquely.

0

精彩评论

暂无评论...
验证码 换一张
取 消