Unable to add contact in group using ABGroupAddMember in iphone?

独自空忆成欢 提交于 2019-12-05 07:38:55

问题


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   

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/5387137/unable-to-add-contact-in-group-using-abgroupaddmember-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!