如何检查HealthKit是否已被授权 [英] How to Check if HealthKit has been authorized
问题描述
我想检查HeathKit是否已获得我授权来读取用户数据,如果我被授权进行锻炼,是否没有弹出警报.但是requestAuthorizationToShareTypes似乎总是返回true吗?如何获得用户是否已授权我的参考?
I want to check if HeathKit has been authorized for me to read the user's data, if I'm authorized segue to the workouts, if not pop an alert. But requestAuthorizationToShareTypes always seems to return true? How can I get a reference to whether the user has authorized me or not?
override func viewDidLoad() {
super.viewDidLoad()
//1. Set the types you want to read from HK Store
let healthKitTypesToRead: [AnyObject?] = [
HKObjectType.workoutType()
]
//2. If the store is not available (for instance, iPad) return an error and don't go on.
if !HKHealthStore.isHealthDataAvailable() {
let error = NSError(domain: "com.myndarc.myrunz", code: 2, userInfo: [NSLocalizedDescriptionKey: "HealthKit is not available in this Device"])
print(error)
let alertController = UIAlertController(title: "HealthKit Not Available", message: "It doesn't look like HealthKit is available on your device.", preferredStyle: .Alert)
presentViewController(alertController, animated: true, completion: nil)
let ok = UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in })
alertController.addAction(ok)
}
//3. Request Healthkit Authorization
let sampleTypes = Set(healthKitTypesToRead.flatMap { $0 as? HKSampleType })
healthKitStore.requestAuthorizationToShareTypes(sampleTypes, readTypes: nil) {
(success, error) -> Void in
if success {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.performSegueWithIdentifier("segueToWorkouts", sender: nil)
});
} else {
print(error)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.showHKAuthRequestAlert()
});
}
}
}
或者,我尝试使用authorizationStatusForType并打开其枚举值,但是存在同样的问题,因为我总是被授权.
Alternatively, I've tried authorizationStatusForType and switched on its enum values but had the same problem in that I'm always authorized.
推荐答案
在这种情况下,您误解了success
标志的含义.
当success
为true时,这意味着iOS成功向用户询问了卫生工具包访问权限.这并不意味着他们回答是".
You are misinterpreting what the success
flag means in this context.
When success
is true, all that means is that iOS successfully asked the user about health kit access. It does NOT mean that they responded to that question with a 'yes'.
要确定他们是否回答是/否,您需要更加具体,然后询问保健工具包是否有权读取/写入您感兴趣的特定类型的数据. 来自HealthKit上的Apple文档:
To determine if they said yes/no, you need to get more specific, and ask health kit if you have permission to read/write the particular type of data you're interested in. From the apple docs on HealthKit:
请求授权后,您的应用即可访问HealthKit存储.如果您的应用有权共享数据类型,则它可以创建和保存该类型的样本.在尝试保存任何示例之前,您应该通过调用AuthorizationStatusForType:来验证您的应用有权共享数据.
After requesting authorization, your app is ready to access the HealthKit store. If your app has permission to share a data type, it can create and save samples of that type. You should verify that your app has permission to share data by calling authorizationStatusForType: before attempting to save any samples.
这篇关于如何检查HealthKit是否已被授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!