加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – HealthKit – requestAuthorization(toShare:read:comp

发布时间:2020-12-14 17:44:07 所属栏目:百科 来源:网络整理
导读:我正在使用 Xcode 8 beta 6,我正在请求访问Health App.请求授权的方法requestAuthorization(toShare:read:completion :)在完成处理程序返回时始终生成true – 在下面的代码中成功.即使我拒绝模拟器中的所有内容,我也会得到真实的结果. 这是我处理授权的代
我正在使用 Xcode 8 beta 6,我正在请求访问Health App.请求授权的方法requestAuthorization(toShare:read:completion :)在完成处理程序返回时始终生成true – 在下面的代码中成功.即使我拒绝模拟器中的所有内容,我也会得到真实的结果.
这是我处理授权的代码.我的代码中的某些内容是错误的还是这是一个Xcode错误?

import Foundation
import HealthKit

class HealthManager {
    private let healthStore = HKHealthStore()

    class var sharedInstance: HealthManager {
        struct Singleton {
            static let instance = HealthManager()
        }
        return Singleton.instance
    }

    private var isAuthorized: Bool? = false

    func authorizeHealthKit(completion: ((_ success: Bool) -> Void)!) {
        let writableTypes: Set<HKSampleType> = [HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.distanceWalkingRunning)!,HKWorkoutType.workoutType(),HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!,HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.activeEnergyBurned)!,HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!]
        let readableTypes: Set<HKSampleType> = [HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.distanceWalkingRunning)!,HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!]

        guard HKHealthStore.isHealthDataAvailable() else {
            completion(false)
            return
        }

        // Request Authorization
        healthStore.requestAuthorization(toShare: writableTypes,read: readableTypes) { (success,error) in

            if success {
                completion(true)
                self.isAuthorized = true
            } else {
                completion(false)
                self.isAuthorized = false
                print("error authorizating HealthStore. You're propably on iPad (error?.localizedDescription)")
            }
        }
    }
}

谢谢你的帮助!

解决方法

你错误地解释了那个成功旗帜意味着什么. YES表示权限屏幕已成功显示,NO表示显示权限屏幕时出错.来自Apple的HealthKit文档:

A Boolean value that indicates whether the request was processed successfully. This value does not indicate whether permission was actually granted. This parameter is NO if an error occurred while processing the request; otherwise,it is YES.

如果要检查是否有权写入数据,则需要使用authorizationStatus(for :),但请注意,您无法确定读取数据的权限.

This method checks the authorization status for saving data.

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. If your app is given share permission but not read permission,you see only the data that your app has written to the store. Data from other sources remains hidden.

文档在这里:https://developer.apple.com/library/ios/documentation/HealthKit/Reference/HKHealthStore_Class/index.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读