< Previous PageNext Page > Hide TOC

Using Address Book from C

This article contains important information for developers using Address Book’s C API. For the most part, the Objective-C API has close method and syntax parity with the C API. This makes it easy to determine, for example, which function corresponds to a given Objective-C method.

There are a couple of primary differences that Carbon developers need to be aware of when using the Address Book C API:

Otherwise, the Objective-C and C APIs act quite similar.

To access the user’s shared address book from Carbon, you need to set an ABAddressBookRef to the return value of ABGetSharedAddressBook:

ABAddressBookRef addressBook = ABGetSharedAddressBook();

Compare this with the same line, but from a Cocoa Objective-C application:

ABAddressBook *addressBook = [ABAddressBook sharedAddressBook];

Notice the similarity in the method and function names. Most of the sample code in this document is written in Objective-C, except where required to explain differences in the API. However, you can see that the mapping between the two APIs is easy to follow.

Let’s take an example from “Searching an Address Book.” Listing 1 searches for anyone named Smith in the current user’s address book and returns an array of results:

Listing 1  Simple Search, in Objective-C

ABAddressBook *AB = [ABAddressBook sharedAddressBook];
 
ABSearchElement *nameIsSmith =
    [ABPerson searchElementForProperty:kABLastNameProperty
                                 label:nil
                                   key:nil
                                 value:@"Smith"
                            comparison:kABEqualCaseInsensitive];
 
NSArray *peopleFound =
    [AB recordsMatchingSearchElement:nameIsSmith];

In Listing 2 you see that very same code segment, but written using the C API:

Listing 2  Simple Search, in C

ABAddressBookRef AB = ABGetSharedAddressBook();
 
ABSearchElementRef nameIsSmith =
    ABPersonCreateSearchElement(kABLastNameProperty,
                    NULL,
                    NULL,
                    CFSTR("Smith"),
                    kABEqualCaseInsensitive);
 
CFArrayRef peopleFound =
    ABCreateArrayOfMatchingRecords(AB, nameIsSmith);

Look familiar? For more details about integrating the Address Book into your Carbon applications, refer to Address Book Reference for C.



< Previous PageNext Page > Hide TOC


© 2002, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-04-04)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.