3

我无法让 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()
    }
}
4

1 回答 1

7

WKRefreshBackgroundTask不要自己更新任何东西,它只会让您的应用程序进入活动状态并运行代码(放置在print("Task received")线路周围的某处),这将更新您的并发症。请记住,WKRefreshBackgroundTasks 的数量是有限的。

并发症可以这样更新:

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)

这将导致系统调用getCurrentTimelineEntry(for:withHandler:)您的方法,CLKComplicationDataSource您可以在其中准备和返回更新的条目。

有关文档中并发症更新的更多信息。更多关于WWDC16 session 中的后台任务。

于 2017-06-26T12:03:06.813 回答