Search code examples
iosswiftcore-motion

CMMotionActivityManager authorization status


I'm trying to figure out a way to handle the authorization statuses for Motion activity

Here's what I came up with so far :

manager = CMMotionActivityManager() 

manager.queryActivityStartingFromDate(now, toDate: now, toQueue: NSOperationQueue.mainQueue(), 
                                                withHandler: { (activities: [CMMotionActivity]?, error: NSError?) -> Void in                

    if(error != nil){
        if(error!.code != Int(CMErrorMotionActivityNotAuthorized.rawValue)){
            print("CMErrorMotionActivityNotAuthorized")
        }else if(error!.code != Int(CMErrorMotionActivityNotEntitled.rawValue)){
            print("CMErrorMotionActivityNotEntitled")
        }else if(error!.code != Int(CMErrorMotionActivityNotAvailable.rawValue)){
            print("CMErrorMotionActivityNotAvailable")
        }
    }
})

One problem though :

When I deny the app permission to motion activity (via settings), I get CMErrorMotionActivityNotEntitled

(I believe I should be getting CMErrorMotionActivityNotAuthorized instead)

Any ideas why ? or at least what's the proper way of doing this ?


Solution

  • Perhaps you are getting CMErrorMotionActivityNotAuthorized. You'll never know, with your code, because your code does not ask what code you are getting. It asks what code you are not getting:

        if(error!.code != Int(CMErrorMotionActivityNotAuthorized.rawValue)){
            print("CMErrorMotionActivityNotAuthorized")
        }else if(error!.code != Int(CMErrorMotionActivityNotEntitled.rawValue)){
            print("CMErrorMotionActivityNotEntitled")
        }else if(error!.code != Int(CMErrorMotionActivityNotAvailable.rawValue)){
            print("CMErrorMotionActivityNotAvailable")
        }
    

    The != operator means is not. So you are doing a series of checks about what the code is not. It's hard to see how you can get any useful information by asking that question. It might make more sense to ask what the code is, which would involve using the == operator.