zafer
zafer

Reputation: 43

CNContactStoreDidChangeNotification not fired on Mac OS X

I am trying to observe if another application such as Contacts.app performs an update on contacts to update contacts list in my app. The app is a Mac OS X application.

I add observer in AppDelegate.swift using

 NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AppDelegate.addressBookDidChange(_:)), name: CNContactStoreDidChangeNotification, object: nil)  

and the selector is

@objc func addressBookDidChange(notification:NSNotification){  
    print("Contacts need update!")  
}.

I do not observe any output although I update a contact using Contacts.app

Am I missing something?

The OS is macOS Sierra Beta 2. I use Contacts framework.

Upvotes: 2

Views: 821

Answers (1)

Max Potapov
Max Potapov

Reputation: 1337

To observe CNContactStore notification you should create this store and keep reference.

fileprivate var CNContactStore: CNContactStore? = nil

func addObserver() {
    contactStore = CNContactStore()
    NSNotificationCenter.default.addObserver(self, selector: #selector(addressBookDidChange(notification:)), name: NSNotification.Name.CNContactStoreDidChange, object: nil)
}

Upvotes: 4

Related Questions