Dimitri T
Dimitri T

Reputation: 951

HealthKit permissions not opening

My app is not opening the HealthKit permissions screen where the user can allow access to Healthkit. I receive the screen on my watch that my app "would like to access your health data". But the phone simply moves to the app launch screen then first page instead of bringing up the settings. There is no error. The app is coded below to request authorisations. Any ideas?

In the phone AppDelegate:

import UIKit
import WatchConnectivity
import HealthKit


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {

var window: UIWindow?
let healthStore = HKHealthStore()


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    WatchSessionManager.sharedManager.startSession()


    return true
}


// authorization from watch
func applicationShouldRequestHealthAuthorization(application: UIApplication) {

    let healthStore = HKHealthStore()

    let quantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)

    let calorieQuantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)

    let dataTypesToRead = NSSet(objects: HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!, HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!)
   healthStore.requestAuthorizationToShareTypes(nil, readTypes: dataTypesToRead as! Set<HKObjectType>, completion: { (success, error) in
        if success {
        } else {
            print(error!.description)
        }
        })

    healthStore.handleAuthorizationForExtensionWithCompletion { success, error in

    }
}

Upvotes: 1

Views: 1744

Answers (2)

Queue li
Queue li

Reputation: 64

Adding NSHealthUpdateUsageDescription in my info.plist fixed the issue for me.

Upvotes: 0

NuN
NuN

Reputation: 408

    let healthKitStore:HKHealthStore = HKHealthStore()

        func authorizeHealthKit(completion: ((_ success:Bool, _ error:NSError?) -> Void)!)
        {   
            //check app is running on iPhone not other devices(iPad does not have health app)
            if !HKHealthStore.isHealthDataAvailable()
            {
                let error = NSError(domain: "DomainName.HealthKitTest", code: 2, userInfo: [NSLocalizedDescriptionKey:"HealthKit is not available in this Device"])
                if completion != nil
                {
                    completion(false, error)
                }
                return;
            }

            healthKitStore.requestAuthorization(toShare: healthKitTypeToWrite() as? Set<HKSampleType>, read: nil) { (success, error) -> Void in

                if completion != nil
                {
                    print("Success: \(success)")
                    print("Error: \(error)")
                    completion?(success, error as NSError?)
                }
            }
        }

        func healthKitTypeToWrite() -> NSSet {
            let typesToWrite = NSSet(objects: HKQuantityType.quantityType(forIdentifier: .dietaryFatTotal),
                                              HKQuantityType.quantityType(forIdentifier: .dietaryFatSaturated),
                                              HKQuantityType.quantityType(forIdentifier: .dietaryCarbohydrates),
                                              HKQuantityType.quantityType(forIdentifier: .dietarySugar),
                                              HKQuantityType.quantityType(forIdentifier: .dietaryProtein),
                                              HKQuantityType.quantityType(forIdentifier: .dietarySodium))
            return typesToWrite
        }

And make sure you add 'NSHealthUpdateUsageDescription' to your info.plist and description saying why you need access to your app.

Hope this will help you.

Upvotes: 4

Related Questions