WWDC 2014 Session 612 (45:14)重点介绍了如何检查 Core Motion Services 的授权状态:
- (void)checkAuthorization:(void (^)(BOOL authorized))authorizationCheckCompletedHandler {
NSDate *now = [NSDate date];
[_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);
});
}];
}
虽然这可行,但第一次调用-queryPedometerDataFromDate:toDate:withHandler:
将通过系统对话框提示用户进行授权。我宁愿检查状态,而不必出于明显的 UX 原因征求用户的许可。
我正在努力实现的目标是可能的,还是我只是以错误的方式思考 API?