NeilMortonNet
NeilMortonNet

Reputation: 1530

iOS - is Motion Activity Enabled in Settings > Privacy > Motion Activity

If an app requires access to Motion Activity data it asks the user at install. However if the user accidentally answers 'No', then the app will not work.

I am looking for a way to check if the Motion Activity is enabled, so that I can prompt the user to enable if not.

Can someone point me in the right direction code wise please?


Following the info from Doc (Thank you), it seems that Apple do not provide a direct method to check the status of Motion Activity in Privacy. I was able to find out by picking up on the error:-

[stepCounter queryStepCountStartingFrom:[NSDate date]
                                     to:[NSDate date]
                                toQueue:[NSOperationQueue mainQueue]
                            withHandler:^(NSInteger numberOfSteps, NSError *error) {
                                if (error != nil && error.code == CMErrorMotionActivityNotAuthorized) {
                                    // The app isn't authorized to use motion activity support.
}

Upvotes: 16

Views: 14337

Answers (5)

Mario Hendricks
Mario Hendricks

Reputation: 757

If you are specifically looking for pedometer (step) data, you should use the CMPedometer authorizationStatus() function, which has been available since iOS 11.

let authorizationStatus = CMPedometer.authorizationStatus()

It will return one of 4 CMAuthorizationStatus values:

  • notDetermined: The user has neither granted nor denied access.
  • restricted: Access is denied due to system-wide restrictions (such as in the privacy settings).
  • denied: The user denied access to your application.
  • authorized: You are authorized to access Motion and Fitness data.

Upvotes: 0

Zaraki
Zaraki

Reputation: 3740

How about this?

- (void)checkMotionAuth {
    switch ([CMMotionActivityManager authorizationStatus])
    {
        case CMAuthorizationStatusNotDetermined:
        {
            CMMotionActivityManager *manager = [[CMMotionActivityManager alloc]init];
            [manager queryActivityStartingFromDate:[NSDate date]
                                            toDate:[NSDate date]
                                           toQueue:[NSOperationQueue mainQueue]
                                       withHandler:^(NSArray<CMMotionActivity *> * _Nullable activities, NSError * _Nullable error) {

                                           if ([CMMotionActivityManager authorizationStatus] == CMAuthorizationStatusAuthorized)
                                           {
                                               NSLog(@"Authorized");
                                           }
                                           else
                                           {
                                               NSLog(@"Denied");
                                           }
                                       }];
        }
            break;
        case CMAuthorizationStatusRestricted:
        case CMAuthorizationStatusDenied:
        {
            NSLog(@"Denied/Restricted");
        }
            break;
        case CMAuthorizationStatusAuthorized:
        {
            NSLog(@"Authorized");
        }
        default:
            break;
    }
 }

Upvotes: 1

Billy
Billy

Reputation: 513

New in iOS 11, CMSSensorRecorder has a static method called authorizationStatus to retrieve it.

+ (CMAuthorizationStatus)authorizationStatus;

Upvotes: 3

Shiyan Xu
Shiyan Xu

Reputation: 1202

New in iOS 9, CMSensorRecorder(doc link) has a class method to check if your app is authorized for Motion & Fitness:

  • Switft class func isAuthorizedForRecording() -> Bool
  • Objective-c + (BOOL)isAuthorizedForRecording

Upvotes: 4

Doc
Doc

Reputation: 1497

Apple has a sample project that shows how to check and request access for all the various permissions (including Motion Activity) here. I suggest you take a quick look at that - it's pretty straightforward.

Upvotes: 15

Related Questions