通用应用程序不支持 HKHealthStore 首选单位?

HKHealthStore preferred units not supported for universal apps?

我有一个引用 HealthKit 的通用 iOS 应用程序。当应用程序安装在 iPhone 上时,应用程序使用 HKHealthStore 检索健康数据,当它安装在 iPad 上时,我通过检查 HKHealthStore.isHealthDataAvailable 是否为假来跳过 HealthKit 查询。这一切都很好,但为了让应用程序 运行 在 iPad 上,我必须从我的应用程序的 .plist "Required device capabilities" 下删除 "healthkit" 条目。这是有道理的,因为 iPad 上没有 healthkit,因此需要它才能使应用程序不会安装在 iPad 上。所有这些都已经在我提交到应用商店并获得批准的应用中完成了。

现在我正在更新应用程序,我想显示用户可能已在 Health 应用程序中手动调整的单位首选项。 HKHeathStore 的文档说您可以使用 preferredUnitsForQuantityTypes 方法执行此操作。但是,从我的 iPhone 应用程序调用此方法会出现以下错误:

Error Domain=com.apple.healthkit Code=4 "Missing com.apple.developer.healthkit entitlement." UserInfo={NSLocalizedDescription=Missing com.apple.developer.healthkit entitlement.}

除了我已经在我的应用程序 ID 中拥有 healthkit 权利。如果我在我的 .plist 的 "Required device capabilities" 部分下添加 "healthkit" entery,那么这个错误就会消失,我会得到想要的结果。但这对我来说不是解决方案,因为我无法在 iPad.

上安装该应用程序

我的问题是,如何支持 iPhone 版本可以成功调用 HKHealthStore.preferredUnitsForQuantityTypes 的通用应用程序?

编辑 这是我的 .entitlements 文件的样子:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.developer.healthkit</key>
    <true/>
    <key>com.apple.security.application-groups</key>
    <array>
        <string>group.com.mycompany.myapp</string>
    </array>
</dict>
</plist>

我发现这里发生了什么。在 HKHealthStore 请求访问健康类型和请求首选单位之间存在线程问题。我不确定为什么 .plist 条目解决了这个问题...但是将首选单位请求移动到 HKHealthStore 的请求访问方法的完成块中解决了这个问题。