Search code examples
swifthealthkit

Works fine when healthkit access was previously authorized on device but crashes otherwise


override func viewDidLoad() {
    super.viewDidLoad()
    ref = Database.database().reference()
    let healthKitTypes: Set = [
        // access step count
        HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
    ]
    healthStore.requestAuthorization(toShare: healthKitTypes, read: healthKitTypes) { (_, _) in
        print("authorized???")
    }
    healthStore.requestAuthorization(toShare: healthKitTypes, read: healthKitTypes) { (bool, error) in
        if let e = error {
            print("oops something went wrong during authorization \(e.localizedDescription)")
        } else {
            print("User has completed the authorization flow")
        }
    }


    getTodaysSteps { (result) in
        print("\(result)")
        self.steps = result

        DispatchQueue.main.async {
            if result == 0 {
                self.StepDisplay.text = " You haven't walked"
            } else {
                self.StepDisplay.text = "\(result)"
            }

        }
    }


    getStepHistory()



}


func getStepHistory() {
    let calendar = Calendar.current
    var interval = DateComponents()
    interval.day = 1

    // Set the anchor date to Monday at 3:00 a.m.
    var anchorComponents = calendar.dateComponents([.day, .month, .year, .weekday], from: Date())


    let offset = (7 + (anchorComponents.weekday ?? 0) - 2) % 7
    anchorComponents.day = (anchorComponents.day ?? 0) - offset
    anchorComponents.hour = 0
    anchorComponents.minute = 1

    guard let anchorDate = calendar.date(from:anchorComponents) else {
        fatalError("*** unable to create a valid date from the given components ***")
    }

    guard let quantityType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount) else {
        fatalError("*** Unable to create a step count type ***")
    }

This code works fine when the authorization is already given on the device. If however, it was not authorized earlier, it will not work unless getStepHistory() is commented out in viewDidLoad. I tried requesting additional authorization from within the getStepHistory() function but it doesn't solve the problem


Solution

  • You need to call getStepHistory inside the completion block to requestAuthorization if it has been authorized.

    healthStore.requestAuthorization(toShare: healthKitTypes, read: healthKitTypes) { (success, error) in
        if let e = error {
            print("oops something went wrong during authorization \(e.localizedDescription)")
        } else if success {
            print("User has granted access")
            getStepHistory()
        } else {
            print("User has completed the authorization flow but there is no access")
        }
    }