HealthKit - requestAuthorization(toShare:read:completion :)总是成功 [英] HealthKit - requestAuthorization(toShare:read:completion:) always succeeds

查看:264
本文介绍了HealthKit - requestAuthorization(toShare:read:completion :)总是成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xcode 8 beta 6而我正在请求访问Health App。当完成处理程序返回时,请求授权的方法 requestAuthorization(toShare:read:completion :) 始终生成 true - 我的代码中的成功。即使我拒绝模拟器中的所有内容,我也会得到 true
这是我处理授权的代码。我的代码中的内容是错误的还是这是一个Xcode错误?

I'm using Xcode 8 beta 6 and I'm requesting access to the 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. Even when I decline everything in the simulator i get a true. Here is my code which handles the authorization. Is something in my code wrong or is this a Xcode bug?

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)")
            }
        }
    }
}

感谢您的帮助!

推荐答案

您误解了成功标志的含义。 YES表示权限屏幕已成功显示,NO表示显示权限屏幕时出错。来自Apple的HealthKit文档:

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. From Apple's HealthKit documentation:


一个布尔值,指示请求是否已成功处理。此值不表示是否实际授予了权限。如果在处理请求时发生错误,则此参数为NO;否则,它是。

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 :) ,但请注意,您无法确定读取数据的授权。

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.


此方法检查授权状态以保存数据。

This method checks the authorization status for saving data.

为了帮助防止敏感健康信息可能泄漏,您的应用无法确定用户是否已授予读取数据的权限。如果未获得许可,则只会显示HealthKit存储中没有所请求类型的数据。如果您的应用获得了共享权限但未获得读取权限,则只会看到应用已写入商店的数据。其他来源的数据仍然隐藏。

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

这篇关于HealthKit - requestAuthorization(toShare:read:completion :)总是成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆