8

I want users to be prompted as soon as app launches for permission to access Motion & Fitness data (CoreMotion).

Right now I'm trying to do a "dummy" query for the data to prompt the permission on a application:didFinishLaunchingWithOptions

CMMotionActivityManager *motionActivityManager=[[CMMotionActivityManager alloc]init];
    [motionActivityManager startActivityUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMMotionActivity *activity) {
        NSLog(@"Dummy query to prompt permission from user");

    }];

But what happens is the app launches and it hangs on the splash screen — if I press home button then the app tries closing and THEN the permission prompt pops in.

Any thought how to accomplish this?

leonsas
  • 4,718
  • 6
  • 43
  • 70

2 Answers2

7

I researched on motion manager and tried to create a library for motion detection. From that, i got to know permission for motion detection will pop up when the below code will execute.

CMMotionActivityManager *motionActivityManager = [[CMMotionActivityManager alloc]init];
    [motionActivityManager startActivityUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMMotionActivity *activity) {
        NSLog(@"Motion...%@",activity);
    }];

Hope this will help you. Thank you.

Sommm
  • 527
  • 4
  • 22
  • 1
    This worked for me after I converted it to Swift. I just had it stop later in my flow but it prompts the request well! – agrippa May 31 '18 at 00:48
1

Had the same issue. The fix that worked for me is to start the motion manager in a place AFTER the first view is fully loaded and has called all of its delegates.

It appears that the method startActivityUpdatesToQueue blocks until it has word one way or another on the permission was accepted but the permission request will not show on the screen until the screen until the first view does. So if you put this method before that time it will simply block the app from finishing the launch and the launch screen will continue.

ztj44
  • 11
  • 2