iosabaddressbookabrecord

How can I create a contact (ABPerson) WITHOUT storing it in the addressbook?


MY GOAL is to allow the users to create a contact using the standard UI (ABPersonViewController) but give them the choice to add it to the addressbook or to store it locally (in my CoreData db). As such, I need to do one of the following:

MY PROBLEM is that I cannot figure out a way to create records without adding them to the user's address book.

SOME CONTEXT

In the Address Book Programming Guide for iOS (link here), Apple's documentation states:

Even though records are usually part of the Address Book database, they can also exist outside of it. This makes them a useful way to store contact information your application is working with.

Which, of course, sounds perfect for what I need to do. But it does not give any instructions about ways to create ABRecords outside of an Addressbook.

The API only seems to provide ways to create then in a specified source (ABPersonCreateInSource) or in the default source (ABPersonCreate). On top of that, although the documentation says to call ABAddressBookAddRecord to add the created record to the addressbook, my tests show that the record is added anyways ! (Maybe it's added by the ABPersonViewController, I haven't checked).

I have searched a lot on internet and, although a few people have asked similar questions, nobody seems to have answered in a way that actually works ;-)

But I've seen several apps do this so I'm confident that it's possible.

MY QUESTION: How would you do this ?


Solution

  • If you don't want to add an address to your address book, when using AddressBook.framework then just create the record (e.g. ABPersonCreate) but do not call ABAddressBookAddRecord followed by ABAddressBookSave, and it won't be stored in your address book. Only if you explicitly add and save the record will it be stored in your address book.

    But, as you surmised, if you use AddressBookUI.framework (either edit and save a record in ABPersonViewController or save record from ABNewPersonViewController), then it will be saved in your address book.

    If you really want to use AddressBookUI.framework, you could, theoretically, use ABNewPersonViewController, and then in your ABNewPersonViewControllerDelegate method newPersonViewController:didCompleteWithNewPerson:, if the ABRecordRef is not NULL, you could then remove that record with ABAddressBookRemoveRecord and save via ABAddressBookSave, and the record that was added would be removed. That strikes me as a particularly inelegant approach, but it works.

    But if you don't need AddressBookUI.framework, then you can very easily create ABRecordRef records and just not explicitly add them to your address book, and you'll be fine.