How to use Extended Runtime Session?

Viewed 426

I'm making an app for Apple Watch that needs extended runtime. I implemented that by writing the below in HostingController, and it works fine for two days of development. But now it stopped working. The app still works, but there is no sign of it running in the background, and the glyph of a running man at the top of the Apple Watch is no longer there. How can that happen?

I wasn't sure that HostingController was the right class to extend with WKExtendedRuntimeSessionDelegate, so I also tried extending ExtensionDelegate instead. Same result: no sign that I had implemented it. Debugging also don't hit those lines.

class HostingController: WKHostingController<AnyView>, WKExtendedRuntimeSessionDelegate {
    func extendedRuntimeSession(_ extendedRuntimeSession: WKExtendedRuntimeSession, didInvalidateWith reason: WKExtendedRuntimeSessionInvalidationReason, error: Error?) {
    // Track when your session starts.
    }

    func extendedRuntimeSessionDidStart(_ extendedRuntimeSession: WKExtendedRuntimeSession) {
    // Finish and clean up any tasks before the session ends.
    }

    func extendedRuntimeSessionWillExpire(_ extendedRuntimeSession: WKExtendedRuntimeSession) {
     // Track when your session ends.
        // Also handle errors here.
    }

    override var body: AnyView {
        return AnyView(ContentView().environmentObject(Settings()))
    }

    let runtimeSession = WKExtendedRuntimeSession();

    override init() {
        super.init()
        runtimeSession.delegate = self;
        runtimeSession.start();
    }
}
0 Answers
Related