I'm working on an iOS application in which I use Motion Activity Manager (in more detail - pedometer). When application launches I need to check is Motion Activity is allowed by user. I do this by doing
_motionActivityManager = [[CMMotionActivityManager alloc] init];
_pedometer = [[CMPedometer alloc] init];
[_pedometer queryPedometerDataFromDate : [NSDate date]
toDate : [NSDate date]
withHandler : ^(CMPedometerData *pedometerData, NSError *error) {
// BP1
if (error != nil) {
// BP2
}
else {
// BP3
}
}];
As discussed here ☛ iOS - is Motion Activity Enabled in Settings > Privacy > Motion Activity
In my understanding this code will trigger "alert window" asking user to opt-in/out.
What happens in my case is that when I run application first time (aka. all warnings are reset), application hangs before 'BP1' (callback is never executed) and then if I stop application with xCode or press home button "alert window" appears. And if I opt-in everything is good, on second run 'BP3' is reached (or 'BP2' if I opt-out).
What I tried do far:
I implemented another way of checking using async execution
[_pedometer queryPedometerDataFromDate : [NSDate date] toDate : [NSDate date] 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); }); }];
This doesn't hang the application, but "alert window" is never showed
- I executed this "checking snippet" in later time in code - but again - application hangs