我想用iPhone在后台更新我的手表应用程序状态 session.updateApplicationContext(applicationContext).
在手表上的应用程序处于活动状态时发送应用程序联系人可以正常工作.
当我激活手表上的主页按钮时,手表应用程序会进入后台,handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>)被呼叫并WKSnapshotRefreshBackgroundTask提供一个.
所以我不明白为什么a WKSnapshotRefreshBackgroundTask被正确触发,但不是a WKWatchConnectivityRefreshBackgroundTask.
Apple的文档说:" 当您从配对的iPhone接收后台数据时,系统会在后台启动您的应用程序,实例化一个WKWatchConnectivityRefreshBackgroundTask对象,并将任务对象传递给您的扩展委托的handleBackgroundTasks:方法.".
但这不会发生在设备上,也不会发生在模拟器上.可能有什么不对?
编辑:
为了检查可能出错的地方,我下载了Apple的演示项目"QuickSwitch",可在此处下载.以下是应该处理后台任务的代码:
func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
for backgroundTask in backgroundTasks {
if let wcBackgroundTask = backgroundTask as? WKWatchConnectivityRefreshBackgroundTask {
// store a reference to the task objects as we might have to wait to complete them
self.wcBackgroundTasks.append(wcBackgroundTask)
} else {
// immediately complete all other task types as we have not added support for them …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用这样的行安排后台任务:
WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate: Date(timeIntervalSinceNow: TimeInterval(5) * 60), userInfo: nil, scheduledCompletion: self.scheduledCompletion)
Run Code Online (Sandbox Code Playgroud)
在哪里
func scheduledCompletion(error: Error?) {
if error == nil { print("successfully scheduled application background refresh") }
else { print("error scheduling background refresh, error: \(error)") }
}
Run Code Online (Sandbox Code Playgroud)
根据文档:
scheduledCompletion在后台应用程序刷新任务完成后由系统调用的块。
但是由于未知原因,它在调度后台刷新任务后被直接调用。后台刷新任务在正确的时间scheduledCompletion被调用,之后不会被调用。
那么它是文档中的错误,WatchKit 中的错误还是我做错了什么?
我无法在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))
}
Run Code Online (Sandbox Code Playgroud)
我也曾尝试从中的handle后台任务方法安排更新,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) …Run Code Online (Sandbox Code Playgroud) 我正在研究watchOS 3应用。
我有一个segue挂在按钮上,并且工作正常。现在,我想以编程方式触发它。
从电话收到消息后,我想将手表导航到特定视图,但似乎无法self.performSegue通过WatchKit 调用method。
有什么办法吗?