我无法让 Apple Watch 复杂功能在 WatchOS 3 中更新/刷新。我在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()
}
}