Home > Software engineering >  MAUI Healthkit Checking Sharing Permissions
MAUI Healthkit Checking Sharing Permissions

Time:12-09

I want to check the healthkit read permissions given by an IOS device.

Inside the appdelegate I have the following. The app loads and brings up the permissions and they save to the device.

private HKHealthStore healthKitStore = new HKHealthStore();

public override void OnActivated(UIApplication application)
{
    ValidateAuthorization();
}

private void ValidateAuthorization()
{
 var restingHeartRate = HKQuantityType.Create(HKQuantityTypeIdentifier.RestingHeartRate);

 var typesToWrite = new NSSet();

 var typesToRead = new NSSet(new[] { restingHeartRate});

 healthKitStore.RequestAuthorizationToShare(
          typesToWrite,
          typesToRead,
          ReactToHealthCarePermissions);
}

void ReactToHealthCarePermissions(bool success, NSError error)
{
   var access = hks.GetAuthorizationStatus(HKQuantityType.Create(HKQuantityTypeIdentifier.RestingHeartRate));
    if (access.HasFlag(HKAuthorizationStatus.SharingAuthorized))
    {
        
    }
}

the returned value from GetAuthorizationStatus can be either, Shared, Notdetermined and Notshared. There's no differentiation between read and write authorization. The code above shows resting heart rate is not shared, even though it has read access.

If I change the above to:

 var typesToWrite = new NSSet(new[] { restingHeartRate});

 var typesToRead = new NSSet();

Then the GetAuthorizationStatus returns as being shared but, I don't want write access, I only want read. Is there another way to check read authorization status only, accurately?

CodePudding user response:

HKAuthorizationStatus gives you write authorization status only. Due to privacy reasons, there is no way to determine read authorization status, as outlined in https://developer.apple.com/documentation/healthkit/protecting_user_privacy

For example, a user could let your app read step count data, but prevent it from reading blood glucose levels. To prevent possible information leaks, an app isn’t aware when the user denies permission to read data. From the app’s point of view, no data of that type exists.

CodePudding user response:

You can't check the user's read permission status, Apple's official documentation says:

To help prevent possible leaks of sensitive health information, your app cannot determine whether or not a user has granted permission to read data. If you are not given permission, it simply appears as if there is no data of the requested type in the HealthKit store. If your app is given share permission but not read permission, you see only the data that your app has written to the store. Data from other sources remains hidden.

For more details, you can check the official documentation:

authorizationStatusForType | Apple

  • Related