BIBIN BENNY
BIBIN BENNY

Reputation: 153

is CNContactStoreDidChange is working ? I'm not able to get callback when contacts are changed

Here is my code, I am registering notification in viewDidLoadwithoptins in appDelegate.

 NotificationCenter.default.addObserver(
                           self, selector: #selector(contactStoreDidChange), name: .CNContactStoreDidChange, object: nil)

and here is my function

 @objc func contactStoreDidChange(notification: NSNotification) {


           print("Contacts Changed")
           UserDefaults.standard.set("Log Changed \(Date())", forKey: "contact")
               }

I am not getting any notifications. I have tried with real device and simulator.

Upvotes: 2

Views: 599

Answers (1)

Rajan Maheshwari
Rajan Maheshwari

Reputation: 14571

As far as I have observed, this notification only gets called when we use the CNContactStore atleast once in our app. So, go to your Screen where your contacts are fetched atleast once.

So basically

let store = CNContactStore()

store.requestAccess(for: .contacts) { (status, error) in
}

The above snippet should be executed once.

Then go to the Contacts app by making your app go to the background, make a change in a contact, and come back to your app. You will see this notification gets fired.

Also make sure to add the observer in some class which exists the whole time, like AppDelegate.

Upvotes: 2

Related Questions