Skip to content

Instantly share code, notes, and snippets.

@KaneCheshire
Created November 27, 2019 11:17
Show Gist options
  • Save KaneCheshire/7cd6b1e775630512fb9e84c66b745cc5 to your computer and use it in GitHub Desktop.
Save KaneCheshire/7cd6b1e775630512fb9e84c66b745cc5 to your computer and use it in GitHub Desktop.
import UIKit
import HealthKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
private let store = HKHealthStore()
private let falls = HKObjectType.quantityType(forIdentifier: .numberOfTimesFallen)!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
requestPermissionToWriteFalls()
return true
}
private func requestPermissionToWriteFalls() {
store.requestAuthorization(toShare: [falls], read: nil) { success, error in
print("Finished requesting authorization", success, error)
self.addAFall()
}
}
private func addAFall() {
let quantity = HKQuantity(unit: .count(), doubleValue: 1)
let sample = HKQuantitySample(type: falls, quantity: quantity, start: Date(), end: Date())
store.save(sample) { success, error in
print("Finished saving fall", success, error)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment