OGeek|极客世界-中国程序员成长平台

标题: ios - CMSensorRecorder 未获得授权,但从未提示我授权? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 22:40
标题: ios - CMSensorRecorder 未获得授权,但从未提示我授权?

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

我的问题:我一直在使用 CMSensorRecorder 对象来记录数据。这工作了一段时间,但 CMSensorRecorder.authorizationStatus() 现在“未授权”。

从一开始,我在 watch 和手机应用程序的 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")
        }



Best Answer-推荐答案


找到 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()

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

关于ios - CMSensorRecorder 未获得授权,但从未提示我授权?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55566272/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4