agencyenterprise / react-native-health

A React Native package to interact with Apple HealthKit
MIT License
861 stars 233 forks source link

How can I detect when a user denies permission or when the current permission is unavailable? #342

Open Poovit-B opened 11 months ago

Poovit-B commented 11 months ago

Describe the bug

  1. First Attempt:

    • Used AppleHealthKit.initHealthKit for the first time.
    • Denied permission, but the response still returns 1.
    • Uncertain whether the user has allowed or not.
  2. Second Attempt:

    • Used AppleHealthKit.initHealthKit for the second time.
    • No permission dialog shown, but the response is still 1.
  3. HealthKit Availability Check:

    • Checked AppleHealthKit.isAvailable, and it returns true.
  4. Permission Status Check:

    • Checked AppleHealthKit.getAuthStatus. Returns {"permissions": {"read": [1, 1], "write": []}}.
    • No permission dialog is displayed; the result is directly returned.
import AppleHealthKit from 'react-native-health';

const PERMS = AppleHealthKit.Constants.Permissions;

export const permissions = {
    permissions: {
        read: [
            PERMS.StepCount,
            PERMS.Steps,
            PERMS.Workout,
            // PERMS.SleepAnalysis,
            // PERMS.ActiveEnergyBurned,
            // PERMS.BasalEnergyBurned,
            // PERMS.EnergyConsumed,
            // PERMS.ActivitySummary,
        ],
        write : []
    }
}
import AppleHealthKit from 'react-native-health';
import moment from 'moment';
import {permissions} from './permissions';

export const checkAppleHealth = async () => {
  return new Promise((resolve, reject) => {
    AppleHealthKit.isAvailable((err, results1) => {
      console.log('isAvailable results', results1);
      if (err) {
        console.log('error isAvailable Healthkit: ', err);
        resolve({success: false, ...results1});
      }
      AppleHealthKit.getAuthStatus(permissions, (err, results2) => {
        console.log('getAuthStatus results', results2);
        if (err) {
          console.log('error getAuthStatus Healthkit: ', err);
          resolve({success: false});
        }

        resolve({success: true, ...results2});
      });
    });
  });
};

export const authAppleHealth = () => {
  return new Promise((resolve, reject) => {
    AppleHealthKit.initHealthKit(permissions, (err, results) => {
      console.log('authAppleHealth results', results);
      console.log('err', err);
      if (err) {
        console.log('error initializing Healthkit: ', err);
        resolve({success: false, response: {error: err?.message}});
      }
      resolve({success: true});
    });
  });
};
LinhLM23496 commented 11 months ago

+1

RameeshSayone commented 6 months ago

+1

nazim-kh4nzm commented 5 months ago

+1

ftaibi commented 1 month ago

please someone fix this I also need to know if the permissions were rejected or not

ozgreenfeld commented 3 weeks ago

Hi, I also ran into this issue, it really crucial to know whether the user approved / denied permissions. I also don't mind helping to add that functionality and open PR, will anyone be available to approve it and release new version?

ozgreenfeld commented 3 weeks ago

After struggling with this a bit I've found out that this data is deliberately not shared by Apple due to privacy reasons. https://developer.apple.com/documentation/healthkit/authorizing_access_to_health_data

As part of the privacy protections, your app doesn’t know whether someone granted or denied permission to read data from HealthKit. If they denied permission, attempts to read data from HealthKit return only samples that your app successfully saved to the HealthKit store.

VipulPithva001 commented 3 weeks ago

We are experiencing an issue where, after initializing AppleHealthKit a second time using initHealthKit, no permission dialog is shown, but the response code remains 1. This behavior occurs consistently.

Please investigate this issue or guide how we might resolve it.