iosswiftxcodecore-motionwatchos-5

CMSensorRecorder is not authorised, but I am never prompted to give authorisation?


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

Solution

  • Found THIS thread for that and there is one answer for that which says:

    I found that the CMSensorRecorder.isAuthorizedForRecording() returns true only after your app is authorized in Privacy/Motion & Fitness (on the iPhone). Then to make the app authorized for Motion & Fitenss I had to access one of the core motion function (like startActivityUpdatesToQueue or even recordAccelerometerForDuration). After that you just need to confirm on the iPhone and from now on CMSensorRecorder.isAuthorizedForRecording() returns true.

    Still, I can't get any data from the CMSensorRecroding. In my case the accelerometerDataFromDate 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:

    1. I am starting the recoding session with recordAccelerometerForDuration(30). Here I record the current date: recordingStartDate = NSDate().

    2. 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.

    3. 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.