HealthKit - requestAuthorization(toShare:read:completion:) 总是成功
HealthKit - requestAuthorization(toShare:read:completion:) always succeeds
我正在使用 Xcode 8 beta 6,我正在请求访问健康应用程序。当我下面的代码中的完成处理程序 returns - success
时,请求授权的方法 requestAuthorization(toShare:read:completion:)
总是生成 true
。即使我拒绝了模拟器中的所有内容,我也会得到 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)!, 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 文档:
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.
我正在使用 Xcode 8 beta 6,我正在请求访问健康应用程序。当我下面的代码中的完成处理程序 returns - success
时,请求授权的方法 requestAuthorization(toShare:read:completion:)
总是生成 true
。即使我拒绝了模拟器中的所有内容,我也会得到 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)!, 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 文档:
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.