EddyVerbruggen / HealthKit

Cordova plugin for the iOS HealthKit framework
MIT License
168 stars 218 forks source link

Request Permissions not working #15

Closed novakben closed 9 years ago

novakben commented 9 years ago

Hi!

I manage to check if HealthKit is available, but the request permission funtion won't fire a permission dialog on the users device. Also, when trying to add a new workout without permissions nothing happens....

Any thoughts?

This is my code:

// after checking window.plugins and window.plugins.healthkit are available, do:
if (window.plugins) {
  window.plugins.healthkit.available(
    function(isAvailable) {
      console.log('HEALTHKIT AVAILABLE, REQUESTING PERMISSIONS')
      window.plugins.healthkit.requestAuthorization(
        {
          'readTypes'  : ['HKCharacteristicTypeIdentifierDateOfBirth', 'HKQuantityTypeIdentifierActiveEnergyBurned', 'HKQuantityTypeIdentifierHeight'],
          'writeTypes' : ['HKQuantityTypeIdentifierActiveEnergyBurned', 'HKQuantityTypeIdentifierHeight', 'HKQuantityTypeIdentifierDistanceCycling']
        },
        console.log('SUCCESS'),
        console.log('ERROR')
      );
    }
  );
}
EddyVerbruggen commented 9 years ago

Hi, what device are you testing this on?

Btw, you should check whether or not isAvailable is true before calling the requestAuthorization function.

novakben commented 9 years ago

Hey Eddy!

iPhone 5 runninng the latest iOS. I'm using HealthKit with other apps so there's no device specific problem.

I've revised the code to check if true, All the console logs get fired, the permission dialog still does'nt

here's the revised code:

// after checking window.plugins and window.plugins.healthkit are available, do:
if (window.plugins) {
  window.plugins.healthkit.available(
    function(isAvailable) {
      if (isAvailable === true) {
        console.log('HEALTHKIT AVAILABLE, REQUESTING PERMISSIONS')
        window.plugins.healthkit.requestAuthorization(
          {
            'readTypes'  : ['HKCharacteristicTypeIdentifierDateOfBirth', 'HKQuantityTypeIdentifierActiveEnergyBurned', 'HKQuantityTypeIdentifierHeight'],
            'writeTypes' : ['HKQuantityTypeIdentifierActiveEnergyBurned', 'HKQuantityTypeIdentifierHeight', 'HKQuantityTypeIdentifierDistanceCycling']
          },
          console.log('SUCCESS'),
          console.log('ERROR')
        );
      }
    }
  );
}
EddyVerbruggen commented 9 years ago

Hmm, can you try removing your app and reinstalling it? Perhaps you're requesting a permission you previously denied. HK doesn't show the permission popup in that case.

novakben commented 9 years ago

Tried it, still nothing :/

EddyVerbruggen commented 9 years ago

Damn.. guessing here.. does your app have the HealthKit entitlement?

novakben commented 9 years ago

Aha! not yet, I havn't created anything in apple developer yet, do I need to do it even if I am not uploading my app to the appstore yet?

EddyVerbruggen commented 9 years ago

To my knowledge: yes. I hope this fixes the problem.

novakben commented 9 years ago

I'll set it up now and let you know!

novakben commented 9 years ago

Ok I created the HealthKit provisions, but that didn't work so I started playing with xCode and I found the culprit! You have to set healthkit in xcode capabiliteis as well.

See attached image screen shot 2015-02-21 at 12 15 38

novakben commented 9 years ago

Now I can get the permission and when trying to write the sample workout I get the permission again to allow to add workouts, after I give permission I don't see that the workout registered on the health kit screen.

That's the code:

 window.plugins.healthkit.saveWorkout({
                  'activityType': 'HKWorkoutActivityTypeCycling',
                  'quantityType': 'HKQuantityTypeIdentifierDistanceCycling',
              'startDate': new Date(), // mandatory
              'endDate': null, // optional, use either this or duration
              'duration': 3600, // in seconds, optional, use either this or endDate
              'energy': 300, //
              'energyUnit': 'kcal', // J|cal|kcal
              'distance': 11, // optional
              'distanceUnit': 'km' // probably useful with the former param
              // 'extraData': "", // Not sure how necessary this is
            },
            console.log('success!!!!'),
            console.log('ERRORR')
        );
EddyVerbruggen commented 9 years ago

Hi, I've just updated the plugin. It now configures the entitlement when installing the plugin. That should fix your trouble next time.

Thanks for reporting this!