oelna
oelna

Reputation: 2436

How to react to CLKComplicationServerActiveComplicationsDidChangeNotification

On the helpful question Force reload watchOS 2 Complications user @alexeyvmp mentions in a comment that you should add an observer for the CLKComplicationServerActiveComplicationsDidChangeNotification event.

What is a good place to create this observer, and what would it look like? Do I create it from my ComplicationDataSource, or in my InterfaceController? How do I make sure it's not recreated over and over?

I have tried to read up on how to create observers in Swift but I'm having a hard time figuring out where to put it. I currently have

let notificationCenter = NSNotificationCenter.defaultCenter()
let mainQueue = NSOperationQueue.mainQueue()

_ = notificationCenter.addObserverForName(CLKComplicationServerActiveComplicationsDidChangeNotification, object: nil, queue: mainQueue) { _ in
    print("active complications changed. refresh!")
}

Any help is appreciated.

Upvotes: 1

Views: 190

Answers (1)

Cosmin
Cosmin

Reputation: 7081

You should place the observer in InterfaceController:

NotificationCenter.default.addObserver( self,
        selector: #selector(InterfaceController.CLKComplicationServerActiveComplicationsDidChangeNotification(_:)),
        name: NSNotification.Name(rawValue: "CLKComplicationServerActiveComplicationsDidChangeNotification"), object: nil )

So you will know in InterfaceController when the complication changed.

Upvotes: 1

Related Questions