我嘗試使用healthkit大致按照本教程(https://www.raywenderlich.com/86336/ios-8-healthkit-swift-getting-started),但要求不同的HKQuantityTypeIdentifier。代碼HealthKitManager類:Swift:'不允許共享以下類型的授權:HKQuantityTypeIdentifierAppleExerciseTime'
import Foundation
import UIKit
import HealthKit
class HealthKitManager {
let healthKitStore:HKHealthStore = HKHealthStore()
func authorizeHealthKit(completion: ((_ success:Bool, _ error:NSError?) -> Void)!)
{
let healthKitTypesToWrite: Set<HKSampleType> = [
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.appleExerciseTime)!
]
let healthKitTypesToRead: Set<HKObjectType> = [
HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.appleExerciseTime)!
]
// If the store is not available (for instance, iPad) return an error and don't go on.
if !HKHealthStore.isHealthDataAvailable()
{
let error = NSError(domain: "com.example", code: 2, userInfo: [NSLocalizedDescriptionKey:"HealthKit is not available in this Device"])
if(completion != nil)
{
completion?(false, error)
}
return;
}
healthKitStore.requestAuthorization(toShare: healthKitTypesToWrite, read: healthKitTypesToRead) { (success, error) -> Void in
completion?(success, error! as NSError)
}
}
}
,並在視圖控制器試圖調用healthKit:調用authorizeHealthKit當我收到錯誤
let healthManager:HealthKitManager = HealthKitManager()
func authorizeHealthKit() {
print("1")
healthManager.authorizeHealthKit { (authorized, error) -> Void in
if authorized {
print("HealthKit authorization received.")
}
else
{
print("HealthKit authorization denied!")
if error != nil {
print("\(error)")
}
}
}
}
不過: 'NSInvalidArgumentException' 的,理由是:「不允許共享以下類型的授權:HKQuantityTypeIdentifierAppleExerciseTime'。打印「1」語句在崩潰前被調用。
感謝您的迴應,我認爲這可能是這種情況,但看不到任何文檔來支持它。那麼使用healthKit無法記錄或添加到appleExerciseTime?如果不是,那麼你知道如何記錄用戶體育鍛煉? –