1

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
Community
  • 1
  • 1
ajitam
  • 371
  • 3
  • 16

1 Answers1

0

Essentially, use can first be sure that the alert view will not block your App, when the first view has appeared, ie. in onViewDidAppear.

For example do:

-(void) viewDidAppear:(BOOL)animated {
    if ([MyActivityManager checkAvailability]) { // motion and activity availability checks
        [myDataManager checkAuthorization:^(BOOL authorized) { // is authorized
            dispatch_async(dispatch_get_main_queue(), ^{
                if (authorized) {
                    // do your UI update etc...
                }
                else {
                    // maybe tell the user that this App requires motion and tell him about activating it in settings...
                }
            });
        }];
    }
}

This is what I do myself. I based my App on the Apple example code as well and noticed, that the example also has the problem you are describing.

mjrehder
  • 317
  • 4
  • 11
  • Did the trick - so bottom line - **make shore that view is loaded before checking is called** – ajitam Feb 12 '15 at 10:49