CMSensorRecorder 未获得授权,但系统从未提示我授予授权?

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

我正在制作一个应用程序,它将记录用户在 Apple Watch 上持续一段时间的加速度,并将该数据发送到配对的 iPhone。我正在使用 CoreMotion 框架来实现这一点。

我的问题:我一直在使用 CMSensorRecorder 对象来记录数据。这工作了一段时间,但 CMSensorRecorder.authorizationStatus() 现在是 "not authorised"。

我从一开始就在手表和 phone 应用程序的 info.plist 文件中有一个 NSMotionUsageDescription。我已经删除并重新添加了这些,但没有运气。

我记得应用程序显示允许运动跟踪的提示,但无法重新创建显示提示的功能。非常感谢有关如何再次启用 CMSensorRecorder 的任何建议。干杯。

我的代码正在初始化 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")
        }

已找到 THIS 个帖子,其中有一个答案是:

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.

所以对于 authorization 我尝试了 startActivityUpdates 如下所示

self.activityManager.startActivityUpdates(to: OperationQueue.main, withHandler: {(data: CMMotionActivity!) -> Void in

})

并且您需要声明 let activityManager = CMMotionActivityManager()

一旦用户允许,if CMSensorRecorder.authorizationStatus() == .authorized { 将是 true