gpt4 book ai didi

ios - 无法在 WatchOS 3 中更新 Apple Watch 并发症

转载 作者:可可西里 更新时间:2023-11-01 00:35:38 28 4
gpt4 key购买 nike

我无法在 WatchOS 3 中更新/刷新 Apple Watch Complication。我在我的 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 的数量是有限的。

Complication 可以这样更新:

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)

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

有关并发症更新的更多信息 in documentation .有关后台任务的更多信息 in WWDC16 session .

关于ios - 无法在 WatchOS 3 中更新 Apple Watch 并发症,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44639722/

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