Gar*_*abo 16 ios swift healthkit
我想检查HeathKit是否已被授权我读取用户的数据,如果我被授权进行训练,如果没有弹出警报.但requestAuthorizationToShareTypes似乎总是返回true?如何获得用户是否授权我的参考?
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()
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
或者,我已尝试使用authorizationStatusForType并打开其枚举值,但遇到了同样的问题,因为我总是被授权.
Nic*_*ick 12
您错误地解释了此success
标志在此上下文中的含义.什么时候success
是真的,这意味着iOS成功地询问了用户有关健康套件的访问权限.这并不意味着他们用'是'回答了这个问题.
要确定他们是否说是/否,您需要更具体,如果您有权读取/写入您感兴趣的特定类型的数据,请询问健康工具包.来自HealthKit上的Apple文档:
请求授权后,您的应用已准备好访问HealthKit商店.如果您的应用具有共享数据类型的权限,则可以创建并保存该类型的样本.在尝试保存任何样本之前,您应该通过调用authorizationStatusForType来验证您的应用是否有权共享数据.
目前,该应用无法确定用户是否已授予读取健康数据的权限。
以下是来自authorizationStatus(for:) 的Apple 描述:
为了帮助防止可能的敏感健康信息泄露,您的应用无法确定用户是否已授予读取数据的权限。如果您没有获得许可,它只会显示为 HealthKit 存储中没有请求类型的数据。如果您的应用被授予共享权限但没有读取权限,您只能看到您的应用写入商店的数据。来自其他来源的数据仍然隐藏。
注意:
authorizationStatus
确定访问状态只是为了写而不是为了读.无法知道您的应用是否具有读取权限.仅供参考,https: //stackoverflow.com/a/29128231/1996294
这是一个请求和检查权限访问的示例 HealthKitStore
// Present user with items we need permission for in HealthKit
healthKitStore.requestAuthorization(toShare: typesToShare, read: typesToRead, completion: { (userWasShownPermissionView, error) in
// Determine if the user saw the permission view
if (userWasShownPermissionView) {
print("User was shown permission view")
// ** IMPORTANT
// Check for access to your HealthKit Type(s). This is an example of using BodyMass.
if (self.healthKitStore.authorizationStatus(for: HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)!) == .sharingAuthorized) {
print("Permission Granted to Access BodyMass")
} else {
print("Permission Denied to Access BodyMass")
}
} else {
print("User was not shown permission view")
// An error occurred
if let e = error {
print(e)
}
}
})
Run Code Online (Sandbox Code Playgroud)