【问题标题】:Is there a way of authorizing my app to access iWatch HealthKit data?有没有办法授权我的应用访问 iWatch HealthKit 数据?
【发布时间】:2019-04-08 10:04:30
【问题描述】:

我有一个 iOS 应用程序,它会在 Fitbit、Mi Band 3 等第三方设备更新时读取 HealthKit 数据(心率)。目前,只要数据源是这些 3rd 方健康设备,我就可以从 HealthKit 以前台和后台模式读取这些数据。但是,当 iWatch 更新 HealthKit 时,我无法读取任何内容。

看来我需要授权我的 iOS 应用访问 iWatch 数据。有一个需要访问数据的所有应用程序的列表。该列表当前为空,这意味着我的应用程序没有此访问权限。请参考附图。

我已经实现了 iOS 应用读取 HealthKit 的授权。我使用“功能”启用了 HealthKit,并将隐私选项添加到 Info.pList 至此,授权iOS读取HealthKit的代码如下:

let readDataTypes : Set = [HKObjectType.quantityType(forIdentifier: .heartRate)!]
        HKStore.requestAuthorization(toShare: nil, read: readDataTypes) { (success, error) in
            guard success else{
                print("error \(String(describing: error))")
                return
            }

我想为我的应用提供从 HeathKit 访问 iWatch 数据的授权。任何文档、链接和指导将不胜感激。

【问题讨论】:

  • 这个方法你查了吗open func handleAuthorizationForExtension(completion: @escaping (Bool, Error?) -> Void)
  • 您好,Prashant,感谢您的回复。我在 iOS 授权后添加了上述功能。但是,授权的 iWatch 列表仍然是空的。是否有更多关于 iOS 应用出现在已批准应用列表中的建议?

标签: ios swift watchkit apple-watch healthkit


【解决方案1】:

你可以这样请求权限:

import UIKit
import HealthKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func touchButton(_ sender: Any) {
        let healthKitStore = HKHealthStore()
        let healthData: Set = [
          HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!
        ]

        healthKitStore.requestAuthorizationToShareTypes(healthData, readTypes: healthData) { _, _ in }
    }
}

【讨论】:

  • 感谢您对马克西姆的评论。但是,您提供的答案与我的代码所做的完全相同,如上所示。这不允许我将我的应用添加到可以访问 iWatch 数据的应用列表中。
【解决方案2】:

我没有专门处理您的问题,但在授权我们的后端 Pryv 读取/写入 HealthKit 中的数据时,我们一直在实施类似的操作。 您可以在这里找到与我们的 HealthKit 桥相关的代码:https://github.com/pryv/bridge-ios-healthkit 我们已经在一个示例应用程序中对其进行了测试,该应用程序允许用户在 Pryv 或 Apple HealthKit 中读取/写入数据并显示它。 你可以在这里找到代码:https://github.com/pryv/app-ios-swift-example。 我们用于个人数据收集和同意管理的中间件是开源的,因此请随意使用它以及我们的示例应用程序:https://github.com/pryv/open-pryv.io

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 2015-07-17
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多