Reputation: 1371
Im working on an training-like app where a started session should keep the app in focus. After about 5 minutes of not raising my wrist, it resigns to background and watch shows the standard complication. The app is still running, but I have to find it in the "dock"
Any ideas on how to prevent the app to loose focus during session? I don't need the screen to be on, but I need the app to show the stats/buttons whenever I raise my wrist.
I have found the
applicationWillResignActive()
but this does not seem to have any functionality to prevent this.
Upvotes: 0
Views: 1994
Reputation: 1371
Thanks to vincent I found out the correct apple docs for this.
Got this to work with a SwiftUI view by wrapping it in a WKHostingController. As seen I just injected that HKWorkoutSession into the SwiftUI view. I then start and stop from inside that view onAppear and onDissapear. Just posting if anyone else is struggling to find a solution. Beware, below is just a concept.
https://developer.apple.com/documentation/healthkit/hkhealthstore
class ExerciseInterfaceController: WKHostingController<Plank> {
var ed = (WKExtension.shared().delegate as! ExtensionDelegate)
let ws: HKWorkoutSession
override init() {
let hs = HKHealthStore()
let cnf = HKWorkoutConfiguration()
cnf.activityType = .mindAndBody. //just for debugging
cnf.locationType = .indoor
do {
ws = try HKWorkoutSession(healthStore: hs, configuration: cnf)
} catch {
fatalError(error.localizedDescription)
}
}
override var body: Plank {
return Plank(workoutsession: ws)
}
Upvotes: 1
Reputation: 267
I think what you need is to use extended runtime sessions, which I think was introduced in watchOS 6.
The extended runtime sessions requires you to specifically list your session type, which I think in your case would qualify, as ‘Physical Therapy’. You are to choose in your project settings’ background modes section
That said, as I do not know what activity the app would be used for, it may also not qualify, if the activity is considered strenuous. In that case, look out for HKWorkoutSession
instead, which kinda treats the workout like the default workouts app one, I believe, but I’m not sure if that’s what you want.
If you want to use the extended runtime sessions, you can:
WKExtendedRuntimeSession()
WKExtendedRuntimeSessionDelegate
)extendedRuntimeSessionDidStart(_ extendedRuntimeSession: WKExtendedRuntimeSession)
to deal with when session is active.Don’t forget to session.start
to start the session, when needed.
Here’s some info that could further help: https://developer.apple.com/documentation/watchkit/using_extended_runtime_sessions
Upvotes: 1