Swift:后台任务在Apple Watch Simulator中有效,但在设备中不起作用

时间:2020-05-08 22:45:34

标签: swift xcode background watch

我制作了Apple Watch Complication以查看回历日期,因此必须每天刷新一次,才能在ComplicationController.swift中做到这一点,我制作了时间表背景,如下所示:

        WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate: date, userInfo: nil) { _ in

        }

然后在ExtensionDelegate.swift中,我像这样处理后台任务:

    func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
    // Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one.
    for task in backgroundTasks {
        // Use a switch statement to check the task type
        switch task {
        case let backgroundTask as WKApplicationRefreshBackgroundTask:
            // Be sure to complete the background task once you’re done.
            ComplicationController().updateComplications()
            backgroundTask.setTaskCompletedWithSnapshot(false)

        case let snapshotTask as WKSnapshotRefreshBackgroundTask:
            // Snapshot tasks have a unique completion call, make sure to set your expiration date
            snapshotTask.setTaskCompleted(restoredDefaultState: true, estimatedSnapshotExpiration: Date.distantFuture, userInfo: nil)
        case let connectivityTask as WKWatchConnectivityRefreshBackgroundTask:
            // Be sure to complete the connectivity task once you’re done.
            connectivityTask.setTaskCompletedWithSnapshot(false)
        case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
            // Be sure to complete the URL session task once you’re done.
            urlSessionTask.setTaskCompletedWithSnapshot(false)
        case let relevantShortcutTask as WKRelevantShortcutRefreshBackgroundTask:
            // Be sure to complete the relevant-shortcut task once you're done.
            relevantShortcutTask.setTaskCompletedWithSnapshot(false)
        case let intentDidRunTask as WKIntentDidRunRefreshBackgroundTask:
            // Be sure to complete the intent-did-run task once you're done.
            intentDidRunTask.setTaskCompletedWithSnapshot(false)
        default:
            // make sure to complete unhandled task types
            task.setTaskCompletedWithSnapshot(false)
        }
    }
}

所有这些在模拟器中都可以正常工作,但在实际设备中却不行! 发生另一件事-我不知道是否相关-:App运行后收到此消息:libMobileGestalt MGIOKitHelper.m:239:无法检索数据

0 个答案:

没有答案