我正在使用Xcode 8 beta 6,並且正在請求訪問Health App。當完成處理程序在我的代碼中返回 - success
時,請求授權的方法requestAuthorization(toShare:read:completion:)
始終會生成true
。即使我拒絕模擬器中的所有內容,我也會收到true
。 這是我處理授權的代碼。是我的代碼錯了,或者這是一個Xcode錯誤?HealthKit - requestAuthorization(toShare:read:completion :)總是成功
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)")
}
}
}
}
感謝您的幫助!
你可以發佈您的最終代碼是什麼樣子? – Adrian