简体   繁体   English

在HealthKit中请求授权:为什么结果总是成功?

[英]Requesting authorization in HealthKit: why the result is always successful?

I´m requesting access to certain health info just for reading this way: 我要求仅通过以下方式访问某些健康信息:

let healthStore = HKHealthStore()
let types: Set<HKObjectType> = [distanceWalkingRunning]

healthStore.requestAuthorization(toShare: nil, read: types) { (success, error) in
        completion(success, error)
}

Then, when launching the app the first time, I've been shown the standard dialog to choose if you want to allow Health to access the info or not. 然后,在首次启动该应用程序时,会向我显示标准对话框,供您选择是否要让Health来访问信息。 If I tap "Don't Allow", then an alert view is displayed saying: 如果我点击“不允许”,则会显示一个警报视图,其中显示:

Health Access - You can turn on health data categories later in the Health app. Health Access-您可以稍后在Health应用程序中打开健康数据类别。

Them, both the alert and the Health access dialog are dismissed and app's landing screen is shown. 他们将关闭警报和“运行状况”访问对话框,并显示应用程序的登录屏幕。 I can see that the completion closure provided in healthStore.requestAuthorization method call is getting a true value for the success argument and a nil for the error argument anyway. 我可以看到, healthStore.requestAuthorization方法调用中提供的completion关闭healthStore.requestAuthorization都为success参数获取了true值,为error参数获取了nil

How could I then know that the user didn't allow the access to Health? 然后,我怎么知道该用户不允许访问“健康”?

The documentation for the success parameter reads: success参数的文档如下:

A Boolean value that indicates whether the request was processed successfully. 一个布尔值,指示是否成功处理了请求。 This value does not indicate whether permission was actually granted. 此值不指示是否实际授予了权限。 This parameter is false if an error occurred while processing the request; 如果在处理请求时发生false则此参数为false否则为false otherwise, it is true . 否则,这是true

In both the initial and subsequent requests, the request was processed successfully and you get true for success . 在初始和后续请求两者的请求被成功处理,你会得到truesuccess

You cannot find out whether the user has granted read access to the data you requested. 您无法确定用户是否已授予对您请求的数据的读取访问权限。 Again, from the documentation : 同样,从文档中

To help prevent possible leaks of sensitive health information, your app cannot determine whether or not a user has granted permission to read data. 为了帮助防止敏感健康信息的可能泄漏,您的应用无法确定用户是否已授予读取数据的权限。 If you are not given permission, it simply appears as if there is no data of the requested type in the HealthKit store. 如果未获得您的许可,则在HealthKit存储中就好像没有所请求类型的数据一样。

After presenting the authorisation request you simply try and read the data and you will either get it or there will appear to be no data available. 提出授权请求后,您只需尝试读取数据,您将得到它,或者似乎没有可用数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM