开发者

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 开发者_C百科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?


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);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜