Skip to content

Instantly share code, notes, and snippets.

@ashokkumarmw
Created April 15, 2018 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashokkumarmw/ce4271db567e4af390c1b290f11b2b77 to your computer and use it in GitHub Desktop.
Save ashokkumarmw/ce4271db567e4af390c1b290f11b2b77 to your computer and use it in GitHub Desktop.
func authorizeHealthKit(completion: @escaping ((_ success: Bool, _ error: MWError?) -> Void)) {
// If the store is not available (for instance, iPad) return an error and don't go on.
if !HKHealthStore.isHealthDataAvailable() {
completion(false, MWError.custom(title: "HealthKit is not available in this Device"))
return;
}
// Request HealthKit authorization
healthKitStore.requestAuthorization(toShare: nil, read: getHealthTypesToRead()) { (success, error) in
DispatchQueue.main.async { // I'm executing this on mainthread because sometimes 'authorizeHealthKit' called on background thread and I need the completion block on main thread as the destination classes have some UI operations.
completion(success, error == nil ? nil : MWError.custom(default: error)) // On this completion block on the other classes, I will start fetching the HealthKit data.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment