5

我有一个苹果手表的复杂功能和 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
}
4

1 回答 1

5

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

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

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

于 2018-12-07T13:29:16.260 回答