hei*_*nia 5 xcode ios swift healthkit xcode8-beta6
我正在使用Xcode 8 beta 6,我正在请求访问Health App.requestAuthorization(toShare:read:completion:)请求授权的方法总是true在完成处理程序返回时产生- success在我的下面的代码中.即使我拒绝模拟器中的所有内容,我也会得到一个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)")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
你错误地解释了那个成功旗帜意味着什么.YES表示权限屏幕已成功显示,NO表示显示权限屏幕时出错.来自Apple的HealthKit文档:
一个布尔值,指示请求是否已成功处理.此值不表示是否实际授予了权限.如果在处理请求时发生错误,则此参数为NO; 否则,是的.
如果要检查是否有权写入数据,则需要使用authorizationStatus(for:),但请注意,您无法确定读取数据的权限.
此方法检查授权状态以保存数据.
为了帮助防止敏感健康信息可能泄漏,您的应用无法确定用户是否已授予读取数据的权限.如果您未获得许可,则看起来好像HealthKit商店中没有所请求类型的数据.如果您的应用获得了共享权限但未获得读取权限,则只会看到应用已写入商店的数据.来自其他来源的数据仍然隐藏.
文档在这里:https://developer.apple.com/library/ios/documentation/HealthKit/Reference/HKHealthStore_Class/index.html
| 归档时间: |
|
| 查看次数: |
2520 次 |
| 最近记录: |