Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C Adding a Contact to a specific Group in the iPhone

I am making an App that stores contacts in the address book , I am using Xcode 4.2

I know how to add a contact in the address book , let s say I have a group called "A" in the Contact list and I want to Add this contact to this group , how to do that ?

here is the Code I am using :

ABAddressBookRef *iPhoneAddressBook = ABAddressBookCreate();
    ABRecordRef contact = ABPersonCreate();

    //add infos
    ABRecordSetValue(contact, kABPersonFirstNameProperty,(__bridge_retained CFStringRef)firstName, nil);
    ABRecordSetValue(contact, kABPersonLastNameProperty,(__bridge_retained CFStringRef)lastName, nil);
    ABRecordSetValue(contact, kABPersonOrganizationProperty, (__bridge_retained CFStringRef)organization, nil);
    ABRecordSetValue(contact, kABPersonJobTitleProperty, (__bridge_retained CFStringRef)title, nil);


    ABMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiRealPropertyType);


    ABMultiValueAddValueAndLabel(multiPhone, (__bridge_retained CFStringRef)workTel, kABWorkLabel, NULL);
    ABMultiValueAddValueAndLabel(multiPhone, (__bridge_retained CFStringRef)workFax, kABPersonPhoneWorkFAXLabel, NULL);

    ABRecordSetValue(contact, kABPersonPhoneProperty, multiPhone, nil);
    CFRelease(multiPhone);
    ABAddressBookAddRecord(iPhoneAddressBook, contact, nil);

Thanks

like image 573
user1051935 Avatar asked Dec 28 '22 10:12

user1051935


1 Answers

First Check if the group exist:

    -(void) CheckIfGroupExistWithName:(NSString*)groupName {


BOOL hasGroup = NO;
//checks to see if the group is created ad creats group for HiBye contacts
ABAddressBookRef addressBook = ABAddressBookCreate();
CFIndex groupCount = ABAddressBookGetGroupCount(addressBook);
CFArrayRef groupLists= ABAddressBookCopyArrayOfAllGroups(addressBook);

    for (int i=0; i<groupCount; i++) {
        ABRecordRef currentCheckedGroup = CFArrayGetValueAtIndex(groupLists, i);
        NSString *currentGroupName = (NSString *)ABRecordCopyCompositeName(currentCheckedGroup);

        if ([currentGroupName isEqualToString:groupName]){
                      //!!! important - save groupID for later use
            self.groupId = ABRecordGetRecordID(currentCheckedGroup);
            hasGroup=YES;
        }
        [groupName release];
    }

    if (hasGroup==NO){
                    //id the group does not exist you can create one
        [self createNewGroup:groupName];
    }

//CFRelease(currentCheckedGroup);
    CFRelease(groupLists);
CFRelease(addressBook); 

}

Use this to create new group, and store its ID

-(void) createNewGroup:(NSString*)groupName {

ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef newGroup = ABGroupCreate();
ABRecordSetValue(HiByeGroup, kABGroupNameProperty,groupName, nil);
ABAddressBookAddRecord(addressBook, newGroup, nil);
ABAddressBookSave(addressBook, nil);
CFRelease(addressBook);

//!!! important - save groupID for later use
self.groupId = ABRecordGetRecordID(newGroup);
CFRelease(newGroup);

}

The is how to set a contact to a group

            //Use the Group ID you stored.
            ABRecordRef HiByeGroup = ABAddressBookGetGroupWithRecordID(addressbook, self.groupId);
    BOOL didAdd = ABGroupAddMember(HiByeGroup,ref,&error);

    if (!didAdd) {
        // Update to handle the error appropriately.
        NSLog(@"Unresolved error while adding person to HiBye group %@", &error);
        exit(-1);  // Fail
    }

    BOOL didSave = ABAddressBookSave(addressbook, &error);

    if (!didSave) {
    // Update to handle the error appropriately.
        NSLog(@"Unresolved error while saving address book%@", &error);
        exit(-1);  // Fail
    }

Good luck

like image 189
shannoga Avatar answered May 08 '23 00:05

shannoga