views:

403

answers:

3

Using the built-in Address Book framework for iPhone, how can I save a specific list of contacts to an array in NSUserDefaults? I need to save a list of recipients for later.

I want to make sure I do not run into problems if the user edits the contacts after my application is closed. Is there some sort of unique ID that each contact has, that I can save and look-up later?

+1  A: 

The ABRecordRef has an ABRecordID property that you should be able to use to uniquely identify it for later access.

Elfred
+1  A: 

Be careful with this, I believe there was an issue (at least in pre OS3.0) where this ABRecordID could change when the user synced their device. This can obviously cause problems if you are relying on using that ID again. I don't know if this is still happening in OS3.0 or not.

http://discussions.apple.com/thread.jspa?threadID=1771736

Just something to watch out for.

Charles Gamble
+1  A: 

ABRecordID's most certainly do change (having learned this the hard way - as a result of multiple machine contact sync)

Matt Gemmell has a nice writeup at iphone-dev-tips-for-synced-contacts

I don't store the composite name, but rather the ABRecordId, first name, last name, an email address and a phone number (the last two of which are used directly by my app).

If I don't get a hit on

 ABRecordRef aRef = ABAddressBookGetPersonWithRecordID(addressBook, aRecordId);

I use a bunch of code to find potential matches on last name, first name and then refine it by phone numbers.

As a final aside, I am associating a generated UUID for the contact so that all of this (potentially) fuzzy resolution is abstracted out of the data I'm associating with a contact.

Jeff Schilling