I'd like my iOS application to interact with Apple's HealKit.
To request authorization I'm using this few lines:
public func requestHealthkit() {
let healthStore = HKHealthStore()
var shareTypes = Set<HKSampleType>()
shareTypes.insert(HKSampleType.workoutType())
var readTypes = Set<HKObjectType>()
readTypes.insert(HKObjectType.workoutType())
healthStore.requestAuthorization(toShare: shareTypes, read: readTypes) { (success, error) -> Void in
if success {
print("[HealthKit] request Authorization succeed!")
} else {
print("[HealthKit] request Authorization failed!")
}
if let error = error { print("[HealthKit] An error occurred: \(error)") }
}
}
Edit 1 (Using this way will not work either) :
healthStore.requestAuthorization(toShare: shareTypes, read: readTypes) { (success, error) in
print("I'm not called. However! :p")
}
Edit 2 (This is my info.plist) :
I have added NSHealthUpdateUsageDescription to the info.plist
-file and turned on health kit capability.
And these are my capabilities settings:
But calling the function requestHealthkit()
will wether work nor produce any error message or log, there just -> N-O-T-H-I-N-G.
Is this the right way to request HealthKit authorization or am I doing something completely wrong? (2. scenario is the very likely case)
Help would be very appreciated, thanks.