cph-cachet / flutter-plugins

A collection of Flutter plugins developed by CACHET
551 stars 681 forks source link

[health 10.1.1] Health.hasPermissions don't works propely #939

Open ifuterman opened 7 months ago

ifuterman commented 7 months ago

iOS emulator version 17.2

Emulator

Please complete the following information for each phone and/or emulator you're experiencing this bug on:

To Reproduce

  1. I manually add the steps with the health app
  2. Start app. In the popup, I restricted access to the health data. The result of the Health().requestAuthorization will be true, and the result of the Health().hasPermissions will be null. The x will be the empty list
  3. Open settings->Health grant access to all data
  4. Start app. The result of the Health().requestAuthorization will be true, and the result of all Health().hasPermissions will be null. The x will be the list with one element
    const dataTypesIOS = <HealthTypeAndPermission>[
    HealthTypeAndPermission(healthType: HealthDataType.SLEEP_ASLEEP, access: HealthDataAccess.READ),
    HealthTypeAndPermission(healthType: HealthDataType.STEPS, access: HealthDataAccess.READ),
    HealthTypeAndPermission(healthType: HealthDataType.HEART_RATE, access: HealthDataAccess.READ),
    HealthTypeAndPermission(healthType: HealthDataType.HEART_RATE_VARIABILITY_SDNN, access: HealthDataAccess.READ),
    HealthTypeAndPermission(healthType: HealthDataType.RESTING_HEART_RATE, access: HealthDataAccess.READ),
    HealthTypeAndPermission(healthType: HealthDataType.FORCED_EXPIRATORY_VOLUME, access: HealthDataAccess.READ),
    HealthTypeAndPermission(healthType: HealthDataType.RESPIRATORY_RATE, access: HealthDataAccess.READ),
    HealthTypeAndPermission(healthType: HealthDataType.EXERCISE_TIME, access: HealthDataAccess.READ),
    ];
List<HealthTypeAndPermission> get dataTypes => Platform.isIOS ? dataTypesIOS : dataTypesAndroid;

Future<bool> authorize() async{
    _isInited = false;
    try{
      bool? permissions = await health.hasPermissions(
        dataTypes.map((e) => e.healthType).toList(),
        permissions: dataTypes.map((e) => e.access).toList()
      );
      if(permissions != null && permissions){
        _isInited = true;
        return true;
      }
      if(permissions == null || !permissions){
        _isInited = await health.requestAuthorization(
            dataTypes.map((e) => e.healthType).toList(),
            permissions: dataTypes.map((e) => e.access).toList()
        );
        //_isInited will be true always
        for(final dataType in dataTypes){
          final result = await health.hasPermissions([dataType.healthType], permissions: [dataType.access]);
          debugPrint('HealthService.authorise $result');
        }
        permissions = await health.hasPermissions(
            dataTypes.map((e) => e.healthType).toList(),
            permissions: dataTypes.map((e) => e.access).toList()
        );
        _isInited = permissions != null ? _isInited : false;
        final x = await health.getHealthDataFromTypes(DateTime.now().subtract(Duration(days: 20)), DateTime.now(), dataTypes.map((e) => e.healthType).toList());
        return _isInited ? true : false;
      }

    }catch (error){
      debugPrint('HealthWrapper.authorise EXCEPTION: ${error.toString()}');
    }
    return false;
  }

Expected behavior

If I have access to the parameter the result of the Health().hasPermissions needs to be not null; the result of the Health().requestAuthorization should depend on the request result.

Actual behavior

The result of the Health().hasPermissions is always null, even if I check specific allowed parameters with read access. And I can read this parameter. Health().requestAuthorization always true

Flutter doctor

[✓] Flutter (Channel stable, 3.19.3, on macOS 14.3.1 23D60 darwin-arm64, locale ru-IL) • Flutter version 3.19.3 on channel stable at /Users/iosiffuterman/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision ba39319843 (4 weeks ago), 2024-03-07 15:22:21 -0600 • Engine revision 2e4ba9c6fb • Dart version 3.3.1 • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/iosiffuterman/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15C500b • CocoaPods version 1.15.2

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] IntelliJ IDEA Ultimate Edition (version 2023.3.6) • IntelliJ at /Applications/IntelliJ IDEA 2.app • Flutter plugin version 78.4.2 • Dart plugin version 233.15026.12

[✓] VS Code (version 1.85.1) • VS Code at /Users/iosiffuterman/Downloads/Visual Studio Code.app/Contents • Flutter extension can be installed from: 🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (4 available) • iPhone Iosif Futerman (mobile) • 00008120-00181D0E0A70201E • ios • iOS 17.3.1 21D61 • iPhone 15 Pro (mobile) • EA6722F7-AF28-448E-8D7E-29F119F72565 • ios • com.apple.CoreSimulator.SimRuntime.iOS-17-2 (simulator) • macOS (desktop) • macos • darwin-arm64 • macOS 14.3.1 23D60 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 123.0.6312.87

[✓] Network resources • All expected network resources are available.

• No issues found!

arsalancy commented 7 months ago

I have the same problem. Did you manage to fix it?

ifuterman commented 7 months ago

I have the same problem. Did you manage to fix it?

No. I downgraded to 9.0.0. For some reason, this version works, but the last doesn`t. I want to show popup, if the data reading restricted for me, because the iOS don't show embedded popup after the first time

Some1Somewhere commented 7 months ago

Any fix?

MoritzMessner commented 7 months ago

hey there, In the documentation for the method hasPermissions you can read the following.

"As Apple HealthKit will not disclose if READ access has been granted for a data type due to privacy concern, this method can only return null to represent an undetermined status, if it is called on iOS with a READ or READ_WRITE access."

samnbd commented 1 week ago

checking if write permission of steps is granted on iOS (Apple health) is actually allowed by Apple, but for some reason Health().hasPermissions([HealthDataType.STEPS], permissions: [HealthDataAccess.WRITE]) always return false even though the write permission was granted

byyoungjin commented 15 hours ago

Having same issue