Swift: 'Authorization to share the following types is disallowed: HKQuantityTypeIdentifierAppleExerciseTime'

Viewed 1436

I am trying to use healthkit roughly following this tutorial (https://www.raywenderlich.com/86336/ios-8-healthkit-swift-getting-started) but requiring a different HKQuantityTypeIdentifier. Code in HealthKitManager class:

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

}

}

and in the ViewController trying to call 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)")
            }
        }
    }
}

However when calling authorizeHealthKit I am getting the error: 'NSInvalidArgumentException', reason: 'Authorization to share the following types is disallowed: HKQuantityTypeIdentifierAppleExerciseTime'. The print "1" statement is called before the crash.

1 Answers
Related