Good day,
Fist I have to be honest. I was not developing for iOS for years, while I wanted to get back to if for quite some time. I started looking into CMWaterSubmresionManager documentation and I simply couldn't make it work. Then I got back to iOS basics to refresh my knowledge. I remembered and learned quite a few things, but still after hours and hours of trying and following various similar other Manager use. I still am not able to make any sense how to get CMWaterSubmresionManager delegate to work. My goal is very very simple - new Xcode project for watchOS app, witch companion app, which would allow me to capture info like water temperature, time underwater etc.
I went through the documentation and did set all the prerequisites, like added authorisation for motion data, extended runtime session etc as per documentation: https://developer.apple.com/documentation/coremotion/accessing_submersion_data
funny enough tried to start monitoring for data in the view. Obviously could set submersionManager.delegate = self, as it "Cannot find 'self' in scope;". Created a class, but could not call it (sorry can't remember the error already) and finally went through "Build a workout app for Apple Watch" ( https://developer.apple.com/wwdc21/10009 ) trying to setup CMWaterSubmerionManager similarly to WorkoutManager and got stuck on on EnviromentalObject Observable something error, which likely does not make any difference, because no such was mentioned anywhere in CMWaterSubmersionManager documentation. I tried to revert from Enviroment object but eventually getting "Constant 'submersionManager' captured by a closure before being initialized", which makes me thing I messed up in so many ways, I cant even track back.
I am totally stuck and can't find any info anywhere on internet, no samples, nothing anywhere.
If anyone could help making it work, I would be very grateful.
My SumbersionManager file looks like:
import CoreMotion
import WatchKit
class SubmersionManager: NSObject, CMWaterSubmersionManagerDelegate, WKExtendedRuntimeSessionDelegate {
func extendedRuntimeSession(_ extendedRuntimeSession: WKExtendedRuntimeSession, didInvalidateWith reason: WKExtendedRuntimeSessionInvalidationReason, error: Error?) {
print("extended session did error")
}
func extendedRuntimeSessionDidStart(_ extendedRuntimeSession: WKExtendedRuntimeSession) {
print("extended session did START")
}
func extendedRuntimeSessionWillExpire(_ extendedRuntimeSession: WKExtendedRuntimeSession) {
print("extended session did EXPIRE")
}
func manager(_ manager: CMWaterSubmersionManager, didUpdate event: CMWaterSubmersionEvent) {
print("didSubmerge??")
}
func manager(_ manager: CMWaterSubmersionManager, didUpdate measurement: CMWaterSubmersionMeasurement) {
print("someMeasurement")
}
func manager(_ manager: CMWaterSubmersionManager, didUpdate measurement: CMWaterTemperature) {
print("some temperature")
}
func manager(_ manager: CMWaterSubmersionManager, errorOccurred error: Error) {
print("some error")
}
func testSinc () {
guard CMWaterSubmersionManager.waterSubmersionAvailable else {
return
}
// Instantiate the submersion manager.
let submersionManager = CMWaterSubmersionManager()
// Assign the submersion manager delegate.
submersionManager.delegate = self
}
func myStartDiveSession() {
// logger.info("*** Starting a dive session. ***")
print("*** Starting a dive session. ***")
// Create the extended runtime session.
let session = WKExtendedRuntimeSession()
// Assign a delegate to the session.
session.delegate = self
// Start the session.
session.start()
}
}
nonisolated func manager(_ manager: CMWaterSubmersionManager, didUpdate event: CMWaterSubmersionEvent) {
let submerged: Bool?
switch event.state {
case .unknown:
submerged = nil
case .notSubmerged:
submerged = false
case .submerged:
print("submerged!!!!!!!!!")
submerged = true
@unknown default:
}
Task {
if let submerged {
print("submerged!!!!")
}
}
}
// Respond to errors.
nonisolated func manager(_ manager: CMWaterSubmersionManager, errorOccurred error: Error) {
}
And I am simply trying to call it from view by initializing
let submersionManager : SubmersionManager
and
.onAppear() {
print("appeared in view")
submersionManager.myStartDiveSession()
OR
submersionManager.testSync()
"and getting mentioned captured by a closure before being initialized" error