简体   繁体   English

HealthKit - requestAuthorization(toShare:read:completion :)总是成功

[英]HealthKit - requestAuthorization(toShare:read:completion:) always succeeds

I'm using Xcode 8 beta 6 and I'm requesting access to the Health App. 我正在使用Xcode 8 beta 6,我正在请求访问Health App。 The method requestAuthorization(toShare:read:completion:) which asks for authorization always produces a true when the completion handler returns - success in my code below. 请求授权的方法requestAuthorization(toShare:read:completion:)在完成处理程序返回时始终生成true - 在下面的代码中success Even when I decline everything in the simulator i get a true . 即使我拒绝模拟器中的所有内容,我也会得到true Here is my code which handles the authorization. 这是我处理授权的代码。 Is something in my code wrong or is this a Xcode bug? 我的代码中的某些内容是错误的还是这是一个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)!, HKWorkoutType.workoutType(), HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!, HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.activeEnergyBurned)!, 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)")
            }
        }
    }
}

Thanks for your help! 谢谢你的帮助!

You're misinterpreting what that success flag means. 你错误地解释了那个成功旗帜意味着什么。 YES means that the permission screen was successfully shown and NO means that there was an error presenting the permissions screen. YES表示权限屏幕已成功显示,NO表示显示权限屏幕时出错。 From Apple's HealthKit documentation: 来自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; 如果在处理请求时发生错误,则此参数为NO; otherwise, it is YES. 否则,是的。

If you want to check if you have access to write data, you need to use authorizationStatus(for:) , but note that you cannot determine authorization for reading data. 如果要检查是否有权写入数据,则需要使用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. 如果您未获得许可,则看起来好像HealthKit商店中没有所请求类型的数据。 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. 来自其他来源的数据仍然隐藏。

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

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

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