【问题标题】:WatchOS5 - how to refresh my complication instantly?WatchOS 5 - 如何立即刷新您的复杂功能?
【发布时间】:2018-11-18 20:00:17
【问题描述】:

我有一个 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
  }
}

我看到这个苹果提供的代码刷新了复杂性。我不确定它是否太多,或者如果我使用上面的条目生成并发症,单独调用 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
}

【问题讨论】:

标签: ios12 apple-watch-complication watchos-5


【解决方案1】:

您应该能够通过从您的 WCSessionDelegate 文件中的 didReceiveApplicationContext 块中调用 refreshComplication() 函数来执行此操作。

因此,如果您通过 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()
        }
    }
}

我的 iOS 应用程序中有一个设置允许用户更改他们的目标,并且使用这种方法几乎可以立即刷新新目标的复杂性。但是,我相信一旦你的并发症用完它的 CPU 预算,什么都不会发生,但希望这不会发生在你身上。见https://developer.apple.com/documentation/clockkit/clkcomplicationserver/1627891-reloadtimeline

希望对您有所帮助,让我知道您的进展情况。 画了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 2015-12-02
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多