I'm trying to work with Core Motion, getting readings from my Apple Watch (Series 4), but am getting inconsistent behaviour.
If I run the code from Apples Core Motion documentation (see Listing 1), I don't get any readings.
The code I run, which works fine on iPhone, but not on Apple Watch, is:
Code Block func startDeviceMotion() { if motion.isDeviceMotionAvailable { self.motion.deviceMotionUpdateInterval = 1.0 / 60.0 self.motion.showsDeviceMovementDisplay = true self.motion.startDeviceMotionUpdates(using: .xMagneticNorthZVertical) self.timer = Timer(fire: Date(), interval: (1.0 / 60.0), repeats: true, block: { (timer) in if let data = self.motion.deviceMotion { let x = data.attitude.pitch let y = data.attitude.roll let z = data.attitude.yaw } }) RunLoop.current.add(self.timer!, forMode: RunLoop.Mode.default) } }
The problem is that although .isDeviceMotionAvailable returns true and runs the if statement correctly, the returned data from self.motion.deviceMotion only gives me nil's.
I.e. DeviceMotion is available but still returns nil.
What I have tried so far without luck, is:
Adding various Accelerometer, Gyroscope, Location, and Privacy entries to my Info.plist
Modifying the code:
lowered the update interval
checking for returned errors from the closure
running it on a different thread
Searched online for example projects, StackOverflow, etc., but without luck
Run the code as an iPhone app. Here it works as expected without issues.