Apple Watch Complication 数据应该存储在哪里?
Where should Apple Watch Complication data be stored?
我需要存储由主要 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.
当他们告诉您将数据缓存在复杂功能可以访问的地方时,他们在想什么?实现此目标的最佳 practice/standard 方法是什么?
您可以在 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
}
}
我需要存储由主要 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.
当他们告诉您将数据缓存在复杂功能可以访问的地方时,他们在想什么?实现此目标的最佳 practice/standard 方法是什么?
您可以在 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
}
}