Kayleigh
Kayleigh

Reputation: 41

Swift - HealthKit will not authenticate

I am trying to make a basic script that will pull my daily steps from HealthKit on iOS, however when the page is triggered - nothing happens. It should request permission to read HealthKit data and I can't work out where I'm going wrong.

Here's my code:

import Foundation
import UIKit
import HealthKit
class HealthKitPage: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    class HealthKitManager {
        let HealthStore = HKHealthStore()

        func AuthoriseHealthKit() -> Bool {
            var isEnabled = true

            if HKHealthStore.isHealthDataAvailable() {
                let StepCount = NSSet(object: HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount))

                let DataTypesToWrite = NSSet(object: StepCount)
                let DataTypesToRead = NSSet(object: StepCount)

                HealthStore.requestAuthorization(toShare: nil, read: (StepCount as! Set<HKObjectType>)) {
                    (success, error) -> Void in
                    isEnabled = success
                }
            }else{
                isEnabled = false
            }

            return isEnabled
        }
    }

Can anyone offer any suggestions?

Upvotes: 1

Views: 263

Answers (2)

Kayleigh
Kayleigh

Reputation: 41

I was able to resolve this using Himali's answer, but it needed converting to Swift 3, here's my working file:

import Foundation
import UIKit
import HealthKit


import UIKit
import HealthKit
class HealthKitPage : UIViewController
{
      let healthStore: HKHealthStore = HKHealthStore()

      override func viewDidLoad()
      {
                var shareTypes = Set<HKSampleType>()

                shareTypes.insert(HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!)



                var readTypes = Set<HKObjectType>()
                readTypes.insert(HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!)


                healthStore.requestAuthorization(toShare: shareTypes, read: readTypes) { (success, error) -> Void in
                          if success {
                                    print("success")
                          } else {
                                    print("failure")
                          }

                          if let error = error { print(error) }


                }





      }

Upvotes: 1

Himali Shah
Himali Shah

Reputation: 189

import UIKit
import HealthKit
class HealthKitPage : UIViewController
{
 let healthStore: HKHealthStore = HKHealthStore()

 override func viewDidLoad()
 {
     var shareTypes = Set<HKSampleType>()

shareTypes.insert(HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!)



        var readTypes = Set<HKObjectType>()
        readTypes.insert(HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!)


        healthStore.requestAuthorizationToShareTypes(shareTypes, readTypes: readTypes) { (success, error) -> Void in
            if success {
                print("success")
            } else {
                print("failure")
            }

            if let error = error { print(error) }
        }


}

}

Upvotes: 0

Related Questions