Home > Software design >  How to run watch app in the background using WKExtendedRuntimeSession
How to run watch app in the background using WKExtendedRuntimeSession

Time:10-17

Thank you for taking a look at this page. I am developing a Watch App and would like to do some background data processing in SwiftUI. I tried to run it using WKExtendedRuntimeSession, but it doesn't seem to be running when it becomes inactive.

import SwiftUI


@main
struct FirebaseExample_Watch_AppApp: App {
    var session = WKExtendedRuntimeSession()

init() {
    session.start()
    
    // MARK:- Extended Runtime Session Delegate Methods
    func extendedRuntimeSessionDidStart(_ extendedRuntimeSession: WKExtendedRuntimeSession) {
        print(1)
    }

    func extendedRuntimeSessionWillExpire(_ extendedRuntimeSession: WKExtendedRuntimeSession) {
        print(2)
    }
        
    func extendedRuntimeSession(_ extendedRuntimeSession: WKExtendedRuntimeSession, didInvalidateWith reason: WKExtendedRuntimeSessionInvalidationReason, error: Error?) {
        print(3)
    }
}


var body: some Scene {
    WindowGroup {
        
        ContentView()
    }
}
}

But the result is nothing happens the moment I put my arm down and the app screen disappears. I alse tried to do this;                  

init() {
            func startTimerButtonPressed() {
        print(WKApplicationState.RawValue())
        var session = WKExtendedRuntimeSession()
        session.start()
        print(WKExtendedRuntimeSessionState.RawValue())
        }

The results are all zeros and do not appear to be initiated.

If you are familiar with WKExtendedRuntimeSession, please let me know how to run it.

Reference https://developer.apple.com/documentation/watchkit/wkextendedruntimesession

CodePudding user response:

You are not setting a delegate for your session. A possible implementation would look like this:

@main
struct TestWatchOSOnly_Watch_AppApp: App {
    
    @State var session = WKExtendedRuntimeSession()
    //define and create the delegate
    @State var delegate = WKDelegate()
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onAppear{
                    //create a new session
                    session = WKExtendedRuntimeSession()
                    //assign the delegate
                    session.delegate = delegate
                    //start the session
                    session.start()
                }
        }
    }
}
// define the delegate and its methods
class WKDelegate: NSObject, WKExtendedRuntimeSessionDelegate{
    
    func extendedRuntimeSession(_ extendedRuntimeSession: WKExtendedRuntimeSession, didInvalidateWith reason: WKExtendedRuntimeSessionInvalidationReason, error: Error?) {
        print(reason.rawValue)
    }
    
    func extendedRuntimeSessionDidStart(_ extendedRuntimeSession: WKExtendedRuntimeSession) {
        print("did start")
    }
    
    func extendedRuntimeSessionWillExpire(_ extendedRuntimeSession: WKExtendedRuntimeSession) {
        print("will expire")
    }
    
    
}
  • Related