For a long time, I searched for a way to access the Mac OS X Address Book framework from Java. This can be easily done by importing a few of the Java libraries, however, this will break the cross platform nature of your code as those libraries are not available on Windows and Linux platforms.

Recently I stumbled upon an alternative solution.

Enter, the ‘Rococoa‘ framework, which magically uses JNI to allow access to underlying Cocoa libraries.

Rococoa is a generic Java binding to the Mac Objective-C object system. It allows the creation and use of Objective-C objects in Java, and the implementation of Objective-C interfaces in Java.

For example, for class ABPerson, we can simply create the following Java class:

You can see from above, any methods that I need from this class are declared as public abstract methods.

Do the same for class ABAddressBook. Now we can start to access contacts!

First we load the native library ‘AddressBook’

Then we get a handle on the Address Book shared instance, and end up with an array of people objects.

Now we loop through all the contacts

Cast the ABPerson object

Now we can get the properties of the record!

Overall, this fantastic tool gives allows Java developers to access virtually anything that you can access from a native Objective-C XCode project. And although it may not be the most elegant solution, it works quite well for most of us!

Let me know what you think in the comments below.