John Canady
John Canady

Reputation: 79

how to create groups in contacts in iPhone apps?

I am developing an iPhone application which handles groups in iPhone contacts, but as my observation there is no facility to create groups in iPhone contacts, but in the SDK we have been given a framework for creating and managing groups, so I decided to create groups from application interface and add contacts to that created groups.

Can anyone help how to create groups in iPhone contacts or is my approach correct to create groups from app?

Upvotes: 1

Views: 2491

Answers (1)

RoIT
RoIT

Reputation: 135

If you have found the solution, suggest to close this question. If not, the following code is for your reference:

- (void)addNewGroup:(NSString *)groupName
{
    ABAddressBookRef addressBook = ABAddressBookCreate();

    ABRecordRef newGroup = ABGroupCreate();

    // Save groupName into ABRecord

    CFErrorRef error = NULL;

    BOOL result = ABRecordSetValue(newGroup, kABGroupNameProperty, (CFTypeRef)groupName, &error);

    if (!result) 
    {
        NSLog(@"Failed to create new group reference with error, %@", error);
        CFRelease(addressBook);
        return;
    }

    result = ABAddressBookAddRecord(addressBook, newGroup, &error);

    if (!result) 
    {
        NSLog(@"Failed to save new group record to address book with error, %@", error);
        CFRelease(addressBook);
        return;
    }

    result = ABAddressBookSave(addressBook,&error); 

    if (!result) 
    {
        NSLog(@"Failed to save change back to address book with error, %@", error);
    }

    CFRelease(addressBook);
}

Upvotes: 1

Related Questions