0

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.

image 1

And these are my capabilities settings:

image 2

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jonas0000
  • 1,085
  • 1
  • 12
  • 36
  • 1
    Make sure you have added `NSHealthUpdateUsageDescription` to your info.plist and turned on your health kit capability. – GIJOW Jan 29 '18 at 20:13
  • I have added all needed Tags to info.plist & of course activated HealthKit @GIJOW. – Jonas0000 Jan 29 '18 at 20:15
  • There is no of course while troubleshooting an issue @Jonas0000. I think you meant HealthKit – GIJOW Jan 29 '18 at 20:16
  • Yeah, you are right. HealthKit - Not AudioKit - sneeze* DEVELOPERS sneeze – Jonas0000 Jan 29 '18 at 20:18
  • Your `healthStore` needs or be a property or it will be deallocated as soon as the function returns and before the user is promoted for access – Paulw11 Jan 29 '18 at 20:23
  • I can not really get your thought @Paulw11. If you thought of declaring just a instance variable like `var healthStore : HKHealthStore?`-this will not solve the problem. Maybe you like to share some example code - would be very appreciated. :-) – Jonas0000 Jan 29 '18 at 20:29
  • That is exactly what I meant. Why wouldn’t it solve the problem? Have you tried – Paulw11 Jan 29 '18 at 20:30
  • Yeah - that's the solution that instantly comes to my mind looking at the problem. Yeah - I was already testing it but it doesn't seems to change anything. I'm absolutely clueless W-H-Y?? @Paulw11 – Jonas0000 Jan 29 '18 at 20:32
  • I tested your code and even without using a property it worked. Have you already run your app and responded to the health access prompt? Once that has happened the user won't be prompted in the app again; they need to change Healthkit access in the Health app. You can delete the app from the device/simulator to re-trigger the prompt, – Paulw11 Jan 29 '18 at 20:51
  • Huh? Thats completely crazy. Yeah I've tested my application on my physic iPhone. I've reinstalled the app nearly a million times but I can not get it working... The status of the permission says in every case `notDetermined`. So would you be so kind to share your **full** code you're using for-Im that clueless? – Jonas0000 Jan 29 '18 at 20:58
  • Are you still there? @Paulw11 – Jonas0000 Jan 29 '18 at 21:36
  • Please stop using the Objective-C tag for Swift questions. Your title states Swift. The code in your question is Swift. Nothing about your question is about the Objective-C programming language. – rmaddy Jan 29 '18 at 22:17
  • @Jonas0000 Here is my code - https://gist.github.com/paulw11/11cc3da5b9502a785073a5cd3c98ef2d I just created a new single view application and added your code into the view controller – Paulw11 Jan 29 '18 at 23:51
  • Thanks for your answer @Paulw11. It seems like you're right. The code is working quiet well. But please have a look at this update to my question [link](https://stackoverflow.com/questions/48528588/unbalanced-calls-to-begin-end-appearance-transitions-swift) :-) – Jonas0000 Jan 30 '18 at 18:58

1 Answers1

0

I have successfully presented the request form. Have a look to this other question (HealthKit - requestAuthorization(toShare:read:completion:) always succeeds). Looks like the types used into your set are not the expected.

override func viewDidAppear(_ animated: Bool) {
    if HKHealthStore.isHealthDataAvailable() {
        if let hk = (UIApplication.shared.delegate as! AppDelegate?)?.healthStore {
            self.requestHealthkit(hk)
        }
    }
}

private func requestHealthkit(_ healthStore:HKHealthStore) {
    let writableTypes: Set<HKSampleType> = [
        HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.distanceWalkingRunning)!,
        HKWorkoutType.workoutType(),
        HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!,
        HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.activeEnergyBurned)!,
        HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!
    ]
    let readableTypes: Set<HKSampleType> = [
        HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.distanceWalkingRunning)!,
        HKWorkoutType.workoutType(),
        HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!,
        HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.activeEnergyBurned)!,
        HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!
    ]


    healthStore.requestAuthorization(toShare: writableTypes, read: readableTypes) { (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)") }
    }
}
  • Hey. Thanks for your answer - It seems like my code is also working but just with this issue (same thing for your code). Please look at this update to my question [link](https://stackoverflow.com/questions/48528588/unbalanced-calls-to-begin-end-appearance-transitions-swift) :-) – Jonas0000 Jan 30 '18 at 18:57