Healthkit Authorisation Status Is Always 1

How to Check if HealthKit has been authorized

You are misinterpreting what the success flag means in this context.
When success is true, all that means is that iOS successfully asked the user about health kit access. It does NOT mean that they responded to that question with a 'yes'.

To determine if they said yes/no, you need to get more specific, and ask health kit if you have permission to read/write the particular type of data you're interested in.
From the apple docs on HealthKit:

After requesting authorization, your app is ready to access the HealthKit store. If your app has permission to share a data type, it can create and save samples of that type. You should verify that your app has permission to share data by calling authorizationStatusForType: before attempting to save any samples.

Requesting authorization in HealthKit: why the result is always successful?

The documentation for the success parameter reads:

A Boolean value that indicates whether the request was processed successfully. This value does not indicate whether permission was actually granted. This parameter is false if an error occurred while processing the request; otherwise, it is true.

In both the initial and subsequent requests, the request was processed successfully and you get true for success.

You cannot find out whether the user has granted read access to the data you requested. Again, from the documentation:

To help prevent possible leaks of sensitive health information, your app cannot determine whether or not a user has granted permission to read data. If you are not given permission, it simply appears as if there is no data of the requested type in the HealthKit store.

After presenting the authorisation request you simply try and read the data and you will either get it or there will appear to be no data available.

Healthkit permission limit to 20

Once an app has prompted for authorization for some types, it cannot prompt again for those types again until authorization is reset for the app. There are two ways you can reset authorization:

  1. Uninstall and re-install the app.
  2. Tap the Reset Location & Privacy button in Settings > General > Reset.

Check if user has authorised steps in HealthKit

The most efficient way to determine whether your app has access to any steps at all is to execute a single HKSampleQuery for steps with a limit of 1 and no predicate or sort descriptors. You don’t need to execute multiple queries for separate periods of time.

Best approach for checking whether HealthKit permissions have been granted on Apple Watch?

I decided to put my HealthKit authorization flow into my App's ExtensionDelegate as opposed to an InterfaceController and in testing so far it appears to work/trigger more reliably, and obviously is more efficient that my original code posted given that I do not request authorization unless it is needed. Would still appreciate any input from others on this. /p>

import WatchKit
import HealthKit
import WatchConnectivity

class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate {

let healthStore = HKHealthStore()
var watchSession: WCSession?
let defaults = UserDefaults.standard

func applicationDidFinishLaunching() {

requestAccessToHealthKit()

}

private func requestAccessToHealthKit() {

let healthKitTypesToWrite: Set<HKSampleType> = [
HKObjectType.workoutType(),
HKSeriesType.workoutRoute(),
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
HKObjectType.quantityType(forIdentifier: .heartRate)!,
HKObjectType.quantityType(forIdentifier: .restingHeartRate)!,
HKObjectType.quantityType(forIdentifier: .bodyMass)!,
HKObjectType.quantityType(forIdentifier: .vo2Max)!,
HKObjectType.quantityType(forIdentifier: .stepCount)!,
HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)!]

let healthKitTypesToRead: Set<HKObjectType> = [
HKObjectType.workoutType(),
HKSeriesType.workoutRoute(),
HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
HKObjectType.quantityType(forIdentifier: .heartRate)!,
HKObjectType.quantityType(forIdentifier: .restingHeartRate)!,
HKObjectType.characteristicType(forIdentifier: .dateOfBirth)!,
HKObjectType.quantityType(forIdentifier: .bodyMass)!,
HKObjectType.quantityType(forIdentifier: .vo2Max)!,
HKObjectType.quantityType(forIdentifier: .stepCount)!,
HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)!]

let authorizationStatus = healthStore.authorizationStatus(for: HKSampleType.workoutType())

switch authorizationStatus {

case .sharingAuthorized: print("sharing authorized")
print("sharing authorized this message is from Watch's extension delegate")

case .sharingDenied: print("sharing denied")

healthStore.requestAuthorization(toShare: healthKitTypesToWrite, read: healthKitTypesToRead) { (success, error) in
print("Successful HealthKit Authorization from Watch's extension Delegate")
}

default: print("not determined")

healthStore.requestAuthorization(toShare: healthKitTypesToWrite, read: healthKitTypesToRead) { (success, error) in
print("Successful HealthKit Authorization from Watch's extension Delegate")
}

}
}
}

Then inside InterfaceController

 @IBAction func didTapStartButton() {

let authorizationStatus = healthStore.authorizationStatus(for: HKSampleType.workoutType())

switch authorizationStatus {
case .sharingAuthorized: print("sharing authorized")
segueToWorkoutInterfaceControllerWithContext()

case .sharingDenied: print("sharing denied")
self.showAlertWith(title: "HealthKit Permission Denied", message: "Please open The App on your iPhone or go to -> Settings -> Privacy -> Health -> App and turn on all permissions")

default: print("not determined")
self.showAlertWith(title: "HealthKit Permission Not Determined", message: "Please open The App on your iPhone or go to -> Settings -> Privacy -> Health -> App and turn on all permissions")
}

}


Related Topics



Leave a reply



Submit