iOS Address Book - How To Know Which Contact Was Added/Edited/Deleted?
Asked Answered
N

2

9

My app needs to store a user's contacts on our servers (given the user's permission of course.) One of it's requirements is to reflect any changes on the devices address book (add/edit/delete) to the server.

Is there an easy way/best practice with regards to determining which address book contacts were changed prior to re-launching an application?

All I can see are callback methods to notify an application of a change in the address book, but it seems there are no documented ways to determine which contacts were added, edited, or deleted.

What I'm thinking of right now is to manually compare the new list of contacts with one stored on the device, then update both the application and the server of the changes. But I think that it might be too much if the user has a big amount of contacts.

Thanks!

Niall answered 17/9, 2012 at 8:16 Comment(1)
I think this STACKOVERFLOW answer might be helpful for you.Popish
A
2

You have to register your class with the ABAddressBookRegisterExternalChangeCallback passing an ABAddressBookRef and the callback ("addressBookDidChange" in my example)

ABAddressBookRef addressBook = //...

ABAddressBookRegisterExternalChangeCallback(addressBook, addressBookDidChange(__bridge_retained  void *)self);

void addressBookDidChange(ABAddressBookRef addressBook, CFDictionaryRef info, void  *context)
{
    //Something changed from last application launch, insert your logic here...

    //If you want to handle it in a "Objective-C" method you can do something like:
    [((__bridge ABManager*) context) yourObjectiveCMethod];

}
Anzio answered 17/8, 2014 at 9:44 Comment(3)
It tells that something has happened with addressbook still doesn't answer - Which Contact Was Added/Edited/Deleted ?Stiffler
@VenuGopalTewari You can save your contacts before and then compare them with the contacts extracted in the callback addressBookDidChangeAnzio
can you suggest some good logic or some example or any sample code.Quasijudicial
S
1

Look into using libsqlite3.dylib and creating a sql database that will access the flat files generated for all the properties needed and then compare your database to the users' devices periodically. Make sure both databases only acquire the bare necessities you will be needing from the abaddressbook framework.

Sample iOS project with sqlite3 library is here:

http://www.techotopia.com/index.php/An_Example_SQLite_based_iOS_7_Application

& Detail on doing so with ABAddressbook for contact's multi-value properties here:

http://linuxsleuthing.blogspot.com/2012/10/addressing-ios6-address-book-and-sqlite.html

Snuffer answered 3/6, 2014 at 13:22 Comment(1)
This is not ideal way to everytime iterate the contacts.. Better is to use ABAddressBookRegisterExternalChangeCallback Cushing

© 2022 - 2024 — McMap. All rights reserved.