gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:33:28 24 4
gpt4 key购买 nike

我需要存储由主要 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com