I wanted to share a couple of brief tips for developers who are working with contacts on iPhone and iPod touch, where the user has enabled syncing with services like MobileMe or Microsoft Exchange.
These are lessons I’ve learned when diagnosing and fixing a couple of bugs which my (most beloved) users of Favorites have reported. If you’re an iPhone developer who’s doing things with the Address Book API, you may want to take note.
Unique record-identifiers change after MobileMe sync
If you’re storing references to the user’s contacts, you’re probably doing so by keeping the record-IDs, on the assumption that these are unique (they are) and will not change for a given contact (wrong). If the user enables MobileMe sync for their contacts, there’s a very high chance that the record-IDs will change. This is presumably to enforce a globally-unique (across all synced devices) identifier in the cloud. It’s also pretty annoying.
One solution would be, when you’re storing a contact’s ID, also grab their full composite name (via
ABRecordCopyCompositeName()) and store that too. When you’re looking up a contact later, if the ID-based look-up fails (
ABAddressBookGetPersonWithRecordID()), fall back on a composite-name-based look-up instead (
ABAddressBookCopyPeopleWithName()). You can also take note of more fields, and do more advanced matching if you feel it’s necessary. If you find a suitable contact, be sure to update your copy of the ID.
It would be nice if Apple fixed this somehow (such as divorcing the device-local unique identifiers from the global cloud-based identifiers), but for now you’ll have to do a little bit of work to ensure your contact-references behave as the user expects.
Labels can be NULL for Exchange contacts
If your user syncs with Microsoft Exchange, the labels (like “work” or “mobile” or whatever) for each phone-number and email address can and often will be NULL (and I mean really NULL, not just an empty string). This can be unexpected, because it’s not possible to create a contact whose numbers/email addresses have a NULL label using the Address Book (Mac) or Contacts (iPhone) UIs.
Just be sure to code defensively, and not assume that a label will always exist even though there’s a number or email address. It’s a very small additional check which you should probably be doing anyway, but I thought it was worth pointing out because it’s perhaps not something you’d otherwise expect.
If you have any other tips relating to dealing with contacts on iPhone from a developer’s perspective, feel free to post them below.