ios - Apple Watch Complication 数据应该存储在哪里?

标签 ios swift watchkit apple-watch-complication

我需要存储由主要 Watch 应用程序(和 iPhone 应用程序)控制并显示在复杂功能中的数据。

official Apple documentation

If you need to fetch or compute the data for your complication, do it in your iOS app or in other parts of your WatchKit extension (for example, by scheduling a background app refresh task), and cache the data in a place where your complication data source can access it.

当他们告诉您将数据缓存在复杂功能可以访问的地方时,他们在想什么?实现这一目标的最佳实践/标准方法是什么?

最佳答案

您可以将一些数据存储在 UserDefaults 中,并从复杂数据源访问这些数据。

即。

//In a background task
func getComplicationData(){
    let yourData = someNetworkCall()
    /* 
    yourData = [
        "complicationHeader": "Some string",
        "complicationInner": "Some other stirng"
    ]


    */
    UserDefaults.standard.set(yourData, forKey: "complicationData")
}

然后在您的 ComplicationDataSource 中

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

    if let yourData = UserDefaults.standard.dictionary(forKey: "complicationData") as? [String: String] {
        //Handle setting up templates for complications
    }

}

关于ios - Apple Watch Complication 数据应该存储在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53981781/

相关文章:

ios - iTunes 连接中的 Beta 测试问题

ios - 如何使 UIBarButton 在禁用时不灰显?

ios - 使用 Gamekit 时如何通过 WiFi 建立连接?

swift - 如何在 Swift 中获取两个日期之间的天数数组?

swift - Apple Watch上的默认本地化语言错误

javascript - 点击在 iPhone 中不起作用

ios - CGContext 的背景图片

ios - 如何在Swift 3中识别editingStyle中的.delete以删除tableview中的单元格

c# - WCSession 发送消息给出错误 "payload could not be delivered"

ios - WKInterfaceTable 中的 WKInterfaceButton 事件处理