10.5. Inserting a Group Entry into the Address Book

Problem

You want to categorize your contacts into groups.

Solution

Use the ABGroupCreate function.

Bear in mind that, as mentioned before, Core Foundation memory management is more complex than what Xcode’s static analyzer could process. Therefore, attempting to use the LLVM compiler to compile Core Foundation code with static analysis turned on might give you a lot of warnings. You can ignore these and test the code with Instruments to make sure your code does not leak, but I encourage you to familiarize yourself with memory management in Core Foundation by reading Apple’s “Memory Management Programming Guide for Core Foundation” document, as mentioned in the previous section.

Discussion

After retrieving the reference to the address book database, you can call the ABGroupCreate function to create a new group entry. However, you must perform a few more operations before you can insert this group into the address book operation. The first thing you have to do is set the name of this group using the ABRecordSetValue function with the kABGroupNameProperty property, as shown in the example code.

After the name of the group is set, add it to the address book database just like you add a new person’s entry—using the ABAddressBookAddRecord function. For more information about adding a new person’s entry to the address book database, please read Recipe 10.4.

Note

Inserting a new group with a name that already exists in the address book database ...

Get iOS 5 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.