0
votes

It says:

Cannot invoke 'requestAuthorizationToShareTypes' with an argument list of type (HKQuantityType, readTypes: HKCharacteristicType, completion: (Bool, NSError!) -> Void)

Any help please

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    if HKHealthStore.isHealthDataAvailable() {
        let healthStore = HKHealthStore()
        healthStore.requestAuthorizationToShareTypes(HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!, readTypes: (HKCharacteristicType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex))! , completion:{
            (success:Bool,error:NSError!) -> Void in
            if !success{
                print("error")
            }
        })
    }

}
2

2 Answers

1
votes

The requestAuthorizationForTypes method expects Sets of HKObjectType. Try this instead:

let shareTypes : Set = [HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!]
let readTypes : Set = [HKCharacteristicType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!]
healthStore.requestAuthorizationToShareTypes(shareTypes, readTypes:readTypes, completion: { (success, error) -> Void in
    if !success{
        print("error")
    }
})
0
votes

Try this approach

// MARK: - HealthKit
func performAutorizationForHealthKit(var completion:((success: Bool, error: NSError!) -> Void)?) {
    let healthKitTypesToShare: Set = [ HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!]
    if !HKHealthStore.isHealthDataAvailable() {
        let error = NSError(domain: "com.mutualCore.healthKit", code: 1, userInfo: [NSLocalizedDescriptionKey : "HealthKit is not available in this Device"])
        completion?(success: false, error: error)
        completion = nil
        return;
    }

    healthKitStore.requestAuthorizationToShareTypes(healthKitTypesToShare, readTypes: [], completion: {
        (successRequest, errorRequest) -> Void in
        completion?(success: successRequest, error: errorRequest)
    })
}

Usage

    performAutorizationForHealthKit { (success, error) -> Void in
        if error != nil {
            print("success")
        }
    }

Also good point to start RayWenderlich tutorial HealthKit