ios - 在提示用户之前确定Core Motion Services(例如M7)的授权状态?
<p><p> <a href="https://developer.apple.com/videos/wwdc/2014/#612" rel="noreferrer noopener nofollow">WWDC 2014 Session 612 (45:14)</a>重点介绍如何检查 Core Motion Services 的授权状态:</p>
<pre><code>- (void)checkAuthorization:(void (^)(BOOL authorized))authorizationCheckCompletedHandler {
NSDate *now = ;
[_pedometer queryPedometerDataFromDate:now toDate:now withHandler:^(CMPedometerData *pedometerData, NSError *error) {
// Because CMPedometer dispatches to an arbitrary queue, it's very important
// to dispatch any handler block that modifies the UI back to the main queue.
dispatch_async(dispatch_get_main_queue(), ^{
authorizationCheckCompletedHandler(!error || error.code != CMErrorMotionActivityNotAuthorized);
});
}];
}
</code></pre>
<p>虽然这可行,但第一次调用 <code>-queryPedometerDataFromDate:toDate:withHandler:</code> 将通过系统对话框提示用户进行授权。我宁愿检查状态,而不必出于明显的用户体验原因征求用户的许可。</p>
<p>我正在努力实现的目标是可能的,还是我只是以错误的方式考虑 API?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>对于 iOS 11:使用 CMPedometer.authorizationStatus() 方法。通过调用此方法,您可以确定您是否被授权、拒绝、限制或未确定。</p>
<p> <a href="https://developer.apple.com/documentation/coremotion/cmpedometer/2913743-authorizationstatus" rel="noreferrer noopener nofollow">https://developer.apple.com/documentation/coremotion/cmpedometer/2913743-authorizationstatus</a> </p>
<p>对于运行 iOS 9-10 的设备,请使用 CMSensorRecorder.isAuthorizedForRecording()。</p>
<p>以下方法适用于所有运行 iOS 9-11 的设备:</p>
<pre><code>var isCoreMotionAuthorized: Bool {
if #available(iOS 11.0, *) {
return CMPedometer.authorizationStatus() == .authorized
} else {
// Fallback on earlier versions
return CMSensorRecorder.isAuthorizedForRecording()
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 在提示用户之前确定Core Motion Services(例如M7)的授权状态?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/28158270/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/28158270/
</a>
</p>
页:
[1]