I have written this code which checks the authorization status of CMMotionActivityManager, and whether the user allows my app to access it.
I soon found that this code does in fact work, but only after iOS 11. As you can see, I have left an else statement for fallback code for versions before iOS 11. Does anyone know how I can achieve the same process for versions below iOS 11 and then use that functionality in the else statement? I cannot find much online.
private func checkAuthorizationStatus() {
if #available(iOS 11.0, *) {
switch CMMotionActivityManager.authorizationStatus() {
case CMAuthorizationStatus.denied:
onStop()
// no authorization
default:break
}
} else {
// Fallback code for < iOS 11
}
}
Any help is appreciated.
There is no way to check the authorization status in iOS versions below iOS 11.
For iOS 10 and less you can simply query the activities. When the app is not authorized to retrieve stored motion data you will get a specific error that you can check for:
// get last 10 days of activity
let endDate = Date()
let startDate = endDate.addingTimeInterval(-60 * 60 * 24 * 10)
let manager = CMMotionActivityManager()
manager.queryActivityStarting(from: startDate, to: endDate, to: .main) { (activities, error) in
if let error = error, (error as NSError).code == CMErrorMotionActivityNotAuthorized.rawValue {
// not autorized!
return
}
// do something with the activities
}