WatchOS5 - 如何立即刷新我的复杂功能?

Ale*_*one 5 apple-watch-complication ios12 watchos-5

我有一个 Apple Watch 并发症和并排运行的 iPhone 应用程序。我在应用程序中有一个按钮可以将应用程序上下文字典传输到手表。我希望看到并发症标题被刷新。

我似乎无法强制执行“点击按钮 - > 查看复杂功能的更新”这种行为。

强制复杂功能更新的适当方法是什么?如何立即刷新我的 Apple Watch 复杂功能?

我确实看到标题发生了变化,但我认为这需要我先点击复杂功能才能打开它的 Apple Watch 应用程序。如何让复杂功能在 Watch 主屏幕上自行更新?

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

if complication.family == .graphicRectangular {
  let template = CLKComplicationTemplateGraphicRectangularLargeImage() 
//...configure
  return template
  }
}
Run Code Online (Sandbox Code Playgroud)

我看到这个苹果提供了刷新复杂性的代码。我不确定它是否太多,或者extendTimeline如果我使用上面的条目生成复杂功能,单独调用是否足够。

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
}
Run Code Online (Sandbox Code Playgroud)

小智 5

您应该能够通过refreshComplication()didReceiveApplicationContext包含WCSessionDelegate的文件中的块调用函数来完成此操作

因此,如果您通过 applicationContext 消息接收标题,您的代码将看起来像这些行。

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()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在我的 iOS 应用程序中有一个设置,允许用户更改他们的目标,使用这种方法几乎立即刷新新目标的复杂性。但是,我相信一旦您的复杂功能用完其 CPU 预算,就不会发生任何事情,但希望这不会发生在您身上。请参阅https://developer.apple.com/documentation/clockkit/clkcomplicationserver/1627891-reloadtimeline

希望有帮助,让我知道你的进展。德鲁