无法在 WatchOS 3 中更新 Apple Watch 并发症
Can't get Apple Watch complication to update in WatchOS 3
我无法在 WatchOS 3 中将 Apple Watch 复杂功能设置为 update/refresh。我在 ComplicationController.swift
文件中使用了以下代码。
func getSupportedTimeTravelDirections(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimeTravelDirections) -> Void) {
handler([.forward])
}
func getTimelineStartDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
handler(Date())
}
func getTimelineEndDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
handler(Date(timeIntervalSinceNow: 60 * 30))
}
我也曾尝试通过 ExtensionDelegate.swift
中的处理后台任务方法安排更新,但它似乎也不起作用。
func scheduleNextRefresh() {
let fireDate = Date(timeIntervalSinceNow: 30 * 60)
let userInfo = ["lastActiveDate" : Date(),
"reason" : "updateWeekNumber"] as Dictionary
WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate: fireDate, userInfo: userInfo as NSSecureCoding) { (error) in
if error == nil {
print("Succesfully updated week number")
}
}
}
func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
for task: WKRefreshBackgroundTask in backgroundTasks {
if WKExtension.shared().applicationState == .background {
if task is WKApplicationRefreshBackgroundTask {
print("Task received")
scheduleNextRefresh()
}
}
task.setTaskCompleted()
}
}
WKRefreshBackgroundTask
本身不会更新任何内容,它只是允许您的应用程序进入活动状态和 运行 代码(位于 print("Task received")
行附近的某处)将更新您的并发症。请记住,WKRefreshBackgroundTask
的数量是有限的。
并发症可以这样更新:
let server = CLKComplicationServer.sharedInstance()
// if you want to add new entries to the end of timeline
server.activeComplications?.forEach(server.extendTimeline)
// if you want to reload all the timeline, which according to snippets looks like your case
server.activeComplications?.forEach(server.reloadTimeline)
这将导致系统调用您的 CLKComplicationDataSource
的 getCurrentTimelineEntry(for:withHandler:)
方法,您可以在其中准备和 return 更新条目。
更多关于并发症的更新in documentation. More about background tasks in WWDC16 session。
我无法在 WatchOS 3 中将 Apple Watch 复杂功能设置为 update/refresh。我在 ComplicationController.swift
文件中使用了以下代码。
func getSupportedTimeTravelDirections(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimeTravelDirections) -> Void) {
handler([.forward])
}
func getTimelineStartDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
handler(Date())
}
func getTimelineEndDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
handler(Date(timeIntervalSinceNow: 60 * 30))
}
我也曾尝试通过 ExtensionDelegate.swift
中的处理后台任务方法安排更新,但它似乎也不起作用。
func scheduleNextRefresh() {
let fireDate = Date(timeIntervalSinceNow: 30 * 60)
let userInfo = ["lastActiveDate" : Date(),
"reason" : "updateWeekNumber"] as Dictionary
WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate: fireDate, userInfo: userInfo as NSSecureCoding) { (error) in
if error == nil {
print("Succesfully updated week number")
}
}
}
func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
for task: WKRefreshBackgroundTask in backgroundTasks {
if WKExtension.shared().applicationState == .background {
if task is WKApplicationRefreshBackgroundTask {
print("Task received")
scheduleNextRefresh()
}
}
task.setTaskCompleted()
}
}
WKRefreshBackgroundTask
本身不会更新任何内容,它只是允许您的应用程序进入活动状态和 运行 代码(位于 print("Task received")
行附近的某处)将更新您的并发症。请记住,WKRefreshBackgroundTask
的数量是有限的。
并发症可以这样更新:
let server = CLKComplicationServer.sharedInstance()
// if you want to add new entries to the end of timeline
server.activeComplications?.forEach(server.extendTimeline)
// if you want to reload all the timeline, which according to snippets looks like your case
server.activeComplications?.forEach(server.reloadTimeline)
这将导致系统调用您的 CLKComplicationDataSource
的 getCurrentTimelineEntry(for:withHandler:)
方法,您可以在其中准备和 return 更新条目。
更多关于并发症的更新in documentation. More about background tasks in WWDC16 session。