Swiftui Apple WatchOS cant figureout how to run it in the background, it suspend on the moment I put down my hand

70 Views Asked by At

I'm trying to create an watch app to stop my habit of touching my lip all the time. I'm using apple core motion and this is my first time writing code in swift on xcode.

The problem I have is everything of the app get suspended on the moment I put down my hand, and will resume if I raise my hand.

Here is my code:

entry file (NoTouchLipAppApp)

import SwiftUI

// 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")
    }
    
    
}


@main
struct NoTouchLipAppApp: 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()
                }
        }
    }
}


ContentView


import SwiftUI
import CoreMotion
import WatchKit

class MotionManager:ObservableObject {
    private let motionManager = CMMotionManager()
    
    @Published var x = 0.0
    @Published var y = 0.0
    @Published var z = 0.0
    
    
    init(){
        motionManager.deviceMotionUpdateInterval = 1/2
        
        
        motionManager.startDeviceMotionUpdates(to:.main){[weak self] data, error in
            guard let motion = data?.attitude else {return}
            self?.x = motion.roll
            self?.y = motion.pitch
            self?.z = motion.yaw
            
            if motion.roll < 0 && motion.pitch < 0{
                WKInterfaceDevice.current().play(.click)

            }
        }
        
    }
    
}


struct ContentView: View {
    @StateObject private var motion = MotionManager()
    
    var body: some View {
        
        VStack{
            Text("\(motion.x)")
            Text("\(motion.y)")
            Text("\(motion.z)")
        }
        
    }
    
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ZStack{
            ContentView()
        }
    }
}

I've enabled background mode and try different options. background options

no luck, then I tried look other possible solutions which suggest me to add some key value to the info.plist. info.plist configures

still no luck.

I'm out of idea how to solve this, please share some light :(

0

There are 0 best solutions below