How to find out, if permission to READ heartrate using HealthKit on watchOS is given?

huangapple go评论70阅读模式
英文:

How to find out, if permission to READ heartrate using HealthKit on watchOS is given?

问题

I understand your request to translate the code portion. Here's the code you provided translated into Chinese:

我有一个`watchOS`应用程序,用户可以跟踪他们的户外活动,包括心率。所有这些都正常工作,但是在支持部门,我们有时会收到用户抱怨应用程序未跟踪他们的心率。几乎所有情况下,缺少对心率的读取权限是问题的原因。现在我想在应用程序内部添加一个小提示,表明缺少此权限,以便用户知道并希望能够自行解决问题。
唯一的问题是...如何?

我尝试了以下方式:
```swift
let healthStore = HKHealthStore()
let heartRateType = HKObjectType.quantityType(forIdentifier: .heartRate)!
let authStatus = healthStore.authorizationStatus(for: heartRateType)
// authStatus将在没有读取权限的情况下仍为.sharingAuthorized,仅具有写入权限

但这只给我提供了SHARE权限状态。我想要的是READ权限状态。
因此,当我在设置中允许写入心率但拒绝读取它时,此authStatus仍然被授权。

而且,尝试启动我的HKAnchoredObjectQuery仅不会在结果处理程序中给我带来错误。它将被调用一次(就像如果我已经授予权限一样),但只带有空样本。但我本来希望出现没有权限的错误?但事实并非如此。

所以我的最后一个问题是:有没有一种方法可以告诉我在watchOS上是否具有读取心率的权限,使用HealthKit


<details>
<summary>英文:</summary>

So I have an `watchOS` app where users can track their outdoor activities and that includes heartrate. All of that is working fine, but in support we sometimes get users that complain that the app is not tracking their heartrate. And in almost all the cases the missing READ permission for heartrate is the problem. Now I&#39;d like to add a little hint inside the app, that this permission is missing so that the users will know and hopefully be able to fix the problem on their own.
The only problem now... How?

I tried the following way:

let healthStore = HKHealthStore()
let heartRateType = HKObjectType.quantityType(forIdentifier: .heartRate)!
let authStatus = healthStore.authorizationStatus(for: heartRateType)
// authStatus will be .sharingAuthorized even WITHOUT read permissions and only write permissions given


but that only gives me the SHARE-permission status. I want the READ-permission status.
So when in settings I allow writing of heartrate but decline reading of it, this `authStatus` is still authorized.

Also just trying to start my `HKAnchoredObjectQuery` will NOT give me an `error` in the result handler. It will be called once (like it would if I had given the permission) but only with empty samples. But I would have expected an error saying I have no permissions?!? But that&#39;s not the case.

So my final question: Is there a way to tell if I have the READ permission for heartrate on `watchOS` using `HealthKit`?

</details>


# 答案1
**得分**: 2

请检查 https://developer.apple.com/documentation/healthkit/authorizing_access_to_health_data

> 为了保护用户的隐私,您的应用程序不知道用户是否授予或拒绝从HealthKit读取数据的权限。如果用户拒绝了权限,尝试从HealthKit查询数据将仅返回您的应用程序成功保存到HealthKit存储的样本。

<details>
<summary>英文:</summary>

check https://developer.apple.com/documentation/healthkit/authorizing_access_to_health_data

&gt; To help protect the user’s privacy, your app doesn’t know whether the
&gt; user granted or denied permission to read data from HealthKit. If the
&gt; user denied permission, attempts to query data from HealthKit return
&gt; only samples that your app successfully saved to the HealthKit store.

</details>



# 答案2
**得分**: 1

所有使用Healthkit的应用程序,如Nike和其他应用程序,只需检查对Healthkit的写入权限,但会请求所有类型的权限。我同意前面的帖子。根据我的经验,大多数用户要么同意所有权限,要么不同意任何权限。这可能是您可以检查权限的唯一方法,或者您可以向Apple提交请求。不过,他们可能会保持不变。

<details>
<summary>英文:</summary>

all the apps that use Healthkit like Nike and others, just check the write permission to Healthkit, but permission is requested for all types. I agree with the previous post about it. Majority of users in my experience just give all permissions or do not give any. That is the only way you can probably check for the permission, or you can file a request to Apple. However they will probably leave it as it is.

</details>



huangapple
  • 本文由 发表于 2023年5月11日 15:27:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定