12

I wants to get notified when there is insert/update event to contacts of iPhone.

Is it possible to get notification to my app regarding specific contact changed event happened ?

Just a newbie... for IOS Swift.

I am not expecting full source code. Just wants to know whether its possible or not and also hint.

Thanks in advance.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Mayur Kataria
  • 675
  • 1
  • 10
  • 14
  • KVO, and NSNotification both are solution for you. See these http://nshipster.com/nsnotification-and-nsnotificationcenter/ and http://www.appcoda.com/understanding-key-value-observing-coding/ – iphonic Mar 23 '15 at 11:15

3 Answers3

32

From iOS 9 you can register your class to observe CNContactStoreDidChangeNotification

NSNotificationCenter.defaultCenter().addObserver(
    self,
    selector: #selector(addressBookDidChange),
    name: NSNotification.Name.CNContactStoreDidChange,
    object: nil)

And then:

@objc func addressBookDidChange(notification: NSNotification){     
    //Handle event here...
}

as reported in Contacts Framework Reference

After a save is successfully executed, the contact store posts a CNContactStoreDidChangeNotification notification to the default notification center. If you cache any Contacts framework objects you need to refetch those objects, either by their identifiers, or with the predicates that were used to originally fetch them, and then release the cached objects. Note that cached objects are stale, but not invalid.

EDIT:

Note that Address Book and Address Book UI frameworks are now deprecated.

Daniel
  • 8,794
  • 4
  • 48
  • 71
andreacipriani
  • 2,519
  • 26
  • 23
  • 2
    I got notification even when nothing in Contacts is changed. If I just open the system contacts and switch to my app, I got notification, and not just one, but two or three – jiawen Dec 13 '16 at 21:50
  • 2
    Will it notify? If we kill the app and change something in addressbook.the open the app again – Usama Sadiq Apr 02 '17 at 07:51
  • 1
    it's not notifying while the app is killed and contacts are changed and then open the app. – Ratul Sharker Apr 10 '18 at 09:00
  • anyone found the solution even when the app is killed and contact is changes as Whatsapp doing? – A.s.ALI Jan 07 '20 at 09:54
7

In iOS it could be done using -

Register the external change call back notifier-

ABAddressBookRef ntificationaddressbook = ABAddressBookCreate();
    ABAddressBookRegisterExternalChangeCallback(ntificationaddressbook, MyAddressBookExternalChangeCallback, self);

Implement the call back -

void MyAddressBookExternalChangeCallback (ABAddressBookRef ntificationaddressbook,CFDictionaryRef info,void *context)
{
    // called when there is any change in AddressBook
}

For more details you can refer this link-

Detect what was changed from ABAddressBookRegisterExternalChangeCallback

Community
  • 1
  • 1
Sanjay Mohnani
  • 5,947
  • 30
  • 46
  • Very well answer @SanjayMohnani !!! I am trying to convert this to swift 2.0 but not able to do it properly. Could you please post the same from swift 2.0 – Vijay Sanghavi Mar 28 '16 at 11:00
0

You can implement KeyValue Observers observevalueforkeypath to get notified for status change of selected elements

Ram Vadranam
  • 485
  • 5
  • 14