Search code examples
ioscocoa-touchcore-motion

Determine if user gave access to the "Motion and Fitness" permission within my iOS app


I'm building an iPhone app (9.0.2) on XCode 7.0. Was wondering what the best way is to determine if a user gave access to the Motion and Fitness permission from within my app.

I'm mainly working with the CMMotionActivityManager class. There is a property called + isActivityAvailable but that returns true even when the Motion and Fitness permission is set to false.

Thanks!


Solution

  • According to Apple's documentation, [CMMotionActivityManager isActivityAvailable] returns a boolean of whether the current device supports motion data, not if the user has given the app permission to use it.

    For permissions, the only way to check whether the user has given permission is through error handling. When the method you are invoking requires permissions, but the app is not authorized by the user, an CMErrorMotionActivityNotAuthorized error code will be thrown.

    From there, you can capture this error and prompt the user to give this app permissions. That's how Apple's sample project (Swift) does it.