菜鸟教程小白 发表于 2022-12-11 22:40:53

ios - CMSensorRecorder 未获得授权,但从未提示我授权?


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

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

<p>从一开始,我在 watch 和手机应用程序的 <code>info.plist</code> 文件中都有一个 <code>NSMotionUsageDescription</code>。我已经删除并重新添加了这些,但没有运气。 </p>

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

<p>我的代码初始化<code>CMSensorRecorder</code>:</p>

<pre><code>if CMSensorRecorder.isAccelerometerRecordingAvailable(){
            if CMSensorRecorder.authorizationStatus() == .authorized {
                print(&#34;\(Date()): recorder started&#34;)
                DispatchQueue.global(qos: .background).async {
                  DispatchQueue.global(qos: .background).sync{
                        self.dateStart = Date()
                        self.recorder.recordAccelerometer(forDuration: self.duration)   
                  }
                }
            }
            else {
                print(&#34;\(CMSensorRecorder.authorizationStatus())&#34;)
                self.xAccLabel.setText(&#34;not authorised&#34;)
            }
      }
      else {
            print (&#34;Recording not available&#34;)
            self.xAccLabel.setText(&#34;Not available&#34;)
      }
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>找到 <a href="https://forums.developer.apple.com/thread/15470" rel="noreferrer noopener nofollow">THIS</a>线程,并且有一个答案说:</p>

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

<p>Still, I can&#39;t get any data from the <code>CMSensorRecroding</code>. In my case the
<code>accelerometerDataFromDate</code> 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:</p>

<ol>
<li><p>I am starting the recoding session with <code>recordAccelerometerForDuration(30)</code>. Here I record the current date:
<code>recordingStartDate = NSDate()</code>.</p></li>
<li><p>I wait more than <code>3min30s</code> (keeping the app on the watch active) and after this time I call:</p>

<p><code>accelerometerDataFromDate(NSDate(timeInterval: 10, sinceDate: recordingStartDate), toDate: NSDate(timeInterval: 20, sinceDate: recordingStartDate)</code></p>

<p>As you can see, I making a 10s window within the requested 30s recording frame.</p></li>
<li><p>I get <code>nil</code>.</p></li>
</ol>

<p>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 <code>accelerometerDataFromDate</code>.</p>

<p>I really wonder how you guys are able to get any readings back from
the sensor recorder...</p>

<p>Maybe things will get better after September 9.</p>
</blockquote>

<p>所以对于 <code>authorization</code> 我尝试了 <code>startActivityUpdates</code> 如下所示</p>

<pre><code>self.activityManager.startActivityUpdates(to: OperationQueue.main, withHandler: {(data: CMMotionActivity!) -&gt; Void in

})
</code></pre>

<p>你需要声明 <code>let activityManager = CMMotionActivityManager()</code></p>

<p>一旦用户允许 <code>如果 CMSensorRecorder.authorizationStatus() == .authorized {</code> 将是 <code>true</code>。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - CMSensorRecorder 未获得授权,但从未提示我授权?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/55566272/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/55566272/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - CMSensorRecorder 未获得授权,但从未提示我授权?