Swift:'不允许共享以下类型的授权:HKQuantityTypeIdentifierAppleExerciseTime'

时间:2017-08-14 18:18:15

标签: ios swift health-kit

我正在尝试大致遵循本教程(https://www.raywenderlich.com/86336/ios-8-healthkit-swift-getting-started)使用healthkit,但需要不同的HKQuantityTypeIdentifier。 HealthKitManager类中的代码:

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)
    }

}

}

并在ViewController中尝试调用healthKit:

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)")
            }
        }
    }
}

然而,在调用authorizeHealthKit时,我收到错误:&#39; NSInvalidArgumentException&#39;,原因:&#39;不允许共享以下类型的授权:HKQuantityTypeIdentifierAppleExerciseTime&#39;。印刷品&#34; 1&#34;在崩溃之前调用语句。

1 个答案:

答案 0 :(得分:2)

您的应用不允许写入HKQuantityType的请求授权,因为该类型的定义是Apple专有的。您应该从healthKitTypesToWrite删除ave