I'm making an app that will record user acceleration for a duration on the Apple Watch and send that data to the paired iPhone. I'm using the CoreMotion
framework to achieve this.
My issue: I've been using a CMSensorRecorder
object to record the data. This worked for a while, but CMSensorRecorder.authorizationStatus()
is now "not authorised".
I've had a NSMotionUsageDescription
in the info.plist
files of both the watch and phone apps since the beginning. I've removed and re-added these, with no luck.
I recall the app displaying a prompt to allow motion tracking, but can't recreate the ability to display the prompt. Would greatly appreciate any advice on how to enable CMSensorRecorder
again. Cheers.
My code initialising CMSensorRecorder
:
if CMSensorRecorder.isAccelerometerRecordingAvailable(){
if CMSensorRecorder.authorizationStatus() == .authorized {
print("\(Date()): recorder started")
DispatchQueue.global(qos: .background).async {
DispatchQueue.global(qos: .background).sync{
self.dateStart = Date()
self.recorder.recordAccelerometer(forDuration: self.duration)
}
}
}
else {
print("\(CMSensorRecorder.authorizationStatus())")
self.xAccLabel.setText("not authorised")
}
}
else {
print ("Recording not available")
self.xAccLabel.setText("Not available")
}
Found THIS thread for that and there is one answer for that which says:
I found that the
CMSensorRecorder.isAuthorizedForRecording()
returnstrue
only after your app is authorized inPrivacy/Motion
&Fitness
(on the iPhone). Then to make the app authorized forMotion
&Fitenss
I had to access one of the core motion function (likestartActivityUpdatesToQueue
or evenrecordAccelerometerForDuration
). After that you just need to confirm on the iPhone and from now onCMSensorRecorder.isAuthorizedForRecording()
returnstrue
.Still, I can't get any data from the
CMSensorRecroding
. In my case theaccelerometerDataFromDate
function does not return any data - the returned value is always nil. Because it is said elsewhere that it can take up to 3mins for the data to become available, I am doing the following scenario:
I am starting the recoding session with
recordAccelerometerForDuration(30)
. Here I record the current date:recordingStartDate = NSDate()
.I wait more than
3min30s
(keeping the app on the watch active) and after this time I call:
accelerometerDataFromDate(NSDate(timeInterval: 10, sinceDate: recordingStartDate), toDate: NSDate(timeInterval: 20, sinceDate: recordingStartDate)
As you can see, I making a 10s window within the requested 30s recording frame.
I get
nil
.I also tried shorter timeouts, accessing the data immediately, and even activating the accelerometer before calling ecordAccelerometerForDuration. Nothing helps, I still get nil back from
accelerometerDataFromDate
.I really wonder how you guys are able to get any readings back from the sensor recorder...
Maybe things will get better after September 9.
So for authorization
I tried startActivityUpdates
like shown below
self.activityManager.startActivityUpdates(to: OperationQueue.main, withHandler: {(data: CMMotionActivity!) -> Void in
})
and you need to declare let activityManager = CMMotionActivityManager()
And once user allow it if CMSensorRecorder.authorizationStatus() == .authorized {
will be true
.