我尝试使用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?如果不是,那么你知道如何记录用户体育锻炼? –