Ios 如何检查HealthKit是否已获得授权
我想检查HeathKit是否授权我读取用户数据,是否授权我继续训练,如果没有,弹出警报。但是requestAuthorizationToShareTypes似乎总是返回true?我如何获得用户是否授权我的参考Ios 如何检查HealthKit是否已获得授权,ios,swift,healthkit,Ios,Swift,Healthkit,我想检查HeathKit是否授权我读取用户数据,是否授权我继续训练,如果没有,弹出警报。但是requestAuthorizationToShareTypes似乎总是返回true?我如何获得用户是否授权我的参考 override func viewDidLoad() { super.viewDidLoad() //1. Set the types you want to read from HK Store let healthKitTypesTo
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并打开了它的枚举值,但遇到了相同的问题,我总是被授权 您误解了
success
标志在此上下文中的含义。
当success
为true时,这意味着iOS成功地向用户询问了有关健康工具包访问的信息。这并不意味着他们对这个问题的回答是“是”
要确定他们是否回答“是/否”,您需要更具体,并询问health kit您是否有权读取/写入您感兴趣的特定类型的数据。
从HealthKit上的apple文档:
请求授权后,您的应用程序即可访问HealthKit商店。如果您的应用程序具有共享数据类型的权限,它可以创建和保存该类型的示例。您应该在尝试保存任何示例之前,通过调用authorizationStatusForType:验证您的应用程序是否具有共享数据的权限
注:authorizationStatus
仅用于确定访问状态
写而不是读。没有选项可以知道你的应用程序是否
读访问。仅供参考
这里是一个在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)
}
}
})
目前,应用程序无法确定用户是否已授予读取健康数据的权限 以下是来自Apple的描述: 为帮助防止敏感健康信息可能泄漏,您的 应用程序无法确定用户是否已授予访问权限 读取数据。如果你没有得到许可,它看起来就像 HealthKit存储中没有请求类型的数据。如果你的 应用程序被授予共享权限,但没有读取权限,您只能看到 应用程序已写入应用商店的数据。来自其他国家的数据 消息来源仍然隐藏
系统从来没有问过你权限的问题?没有,但即使你没有授权,成功块也会被称为谢谢你…我尝试过authorizationStatusForType,但据我所知,我看不出这一点:苹果关心他们用户的隐私,如果你没有被授予权限,它看起来就像HealthKit存储中没有请求类型的数据一样。如果您的应用被授予共享权限,但没有读取权限,则您只能看到应用已写入存储的数据。来自其他来源的数据仍然隐藏。
requestAuthorization
显示授权视图。。。这不是检查。我已启用读取和写入权限,但authorizationStatus始终返回false@SarathNagesh你有没有发现每次都是假的运气?我也是。如果有,请分享解决方案。我失去了我的一天。