Есть ли надежный способ продлить сроки осложнений на AppleWatch, если приложение не запущено - PullRequest
1 голос
/ 11 февраля 2020

Я пытаюсь удлинить график времени осложнений от фона, но самое позднее после 1 или 2 продления срока он больше не работает, и содержание осложнения больше не обновляется (у меня осложнение с новое значение каждую минуту).

Приложение доступно только для часов.

Код моего фона refre sh:

import WatchKit

class ExtensionDelegate: NSObject, WKExtensionDelegate {

    let defaults = UserDefaults.standard

    func applicationDidFinishLaunching() {
        self.scheduleBackgroundRefresh()
    }

    func applicationDidBecomeActive() {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillResignActive() {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, etc.
    }

    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:
                self.scheduleBackgroundRefresh()

                let server = CLKComplicationServer.sharedInstance()

                for complication in server.activeComplications ?? [] {
                    server.extendTimeline(for: complication)
                }
                backgroundTask.setTaskCompletedWithSnapshot(false)
            default:
                // make sure to complete unhandled task types
                task.setTaskCompletedWithSnapshot(false)
            }
        }
    }

}

extension ExtensionDelegate{
    private func scheduleBackgroundRefresh(){
        let fireDate = Date().addingTimeInterval(TimeInterval(60*60))
        let userInfo = ["reason" : "complication extendTimeline"] as NSDictionary
        WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate: fireDate, userInfo: userInfo){
            (error) in
            if error == nil {
                NSLog("Background refresh scheduled")
            }
        }
    }
}

У вас есть какие-либо идея, почему фоновое приложение refre sh не работает постоянно? Или может быть проблема в методе обновления осложнений, .extendTimeline(for:?

...