WatchOS5 - how to refresh my complication instantly?

1.9k views Asked by At

I have an apple watch complication and the iPhone app running side by side. I have a button within the app to transmit application context dictionary to the watch. I expect to see the complication title to be refreshed.

I cannot seem to force the "tap button -> see update on the complication" kind of behavior.

What is the appropriate method to force a complication update? How can I refresh my apple watch complication instantly?

I do see the title changes, but I think it requires me to tap on the complication to open it's apple watch app first. How can I get the complication to update itself on the Watch home screen?

func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {

if complication.family == .graphicRectangular {
  let template = CLKComplicationTemplateGraphicRectangularLargeImage() 
//...configure
  return template
  }
}

I see this apple provided code that refreshes the complication. I'm not sure if it is too much, or if calling extendTimeline alone is sufficient if I'm generating the complication using the entry above.

func refreshComplication() {
      #if os(watchOS)
    let server = CLKComplicationServer.sharedInstance()
    if let complications = server.activeComplications {
        for complication in complications {
            // Call this method sparingly. If your existing complication data is still valid,
            // consider calling the extendTimeline(for:) method instead.
            server.reloadTimeline(for: complication)
        }
    }
    #endif
}
1

There are 1 answers

0
Drew Westcott On

You should be able to do this by calling the refreshComplication() function from your didReceiveApplicationContext block in the file which has your WCSessionDelegate.

So if you are receiving the title via an applicationContext message your code would look something along these lines.

func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {

    if let updatedTitle = applicationContext["updatedTitle"] {
        if let title = updateTitle as? String {
            //Remeber that complicationServer.swift is a seperate process therefore you will need to store the received data somehow.
            UserDefaults.standard.set(title, forKey: "complicationTitle")
            refreshComplication()
        }
    }
}

I have a setting in my iOS App that lets the user change their target and using this method refreshed the complication with the new target almost instantly. However, I believe once your complication has used up its cpu budget nothing will happen, but hopefully that is not happening for you. See https://developer.apple.com/documentation/clockkit/clkcomplicationserver/1627891-reloadtimeline

Hope that helps, let me know how you get on. Drew