Plugin.Maui.Health
provides the ability to access personal health data in your .NET MAUI application.
Available on NuGet.
Install with the dotnet CLI: dotnet add package Plugin.Maui.Health
, or through the NuGet Package Manager in Visual Studio.
Plugin.Maui.Health
provides the HealthDataProvider
class that has a single property Property
that you can get or set.
You can either use it as a static class, e.g.: Health.Default.Property = 1
or with dependency injection: builder.Services.AddSingleton<IHealth>(Health.Default);
Platform | Minimum Version Supported |
---|---|
iOS | 14.0+ |
Android | currently not supported |
Before you can start using Health, you will need to request the proper permissions on each platform.
You need to add permissions in your Info.plist file to read/write to the HealthKit store.
<key>NSHealthShareUsageDescription</key>
<string>We need access to your health data to read your steps and other metrics.</string>
<key>NSHealthUpdateUsageDescription</key>
<string>We need access to write your steps and other metrics.</string>
Add ´Entitlements.plist´ to you iOS platform project.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.healthkit</key>
<true/>
</dict>
</plist>
You will first need to register the Health
with the MauiAppBuilder
following the same pattern that the .NET MAUI Essentials libraries follow.
builder.Services.AddSingleton(Health.Default);
You can then enable your classes to depend on IHealth
as per the following example.
public class HealthViewModel
{
readonly IHealth Health;
public HealthViewModel(IHealth Health)
{
this.Health = Health;
}
}
Alternatively if you want to skip using the dependency injection approach you can use the Health.Default
property.
public class HealthViewModel
{
public async Task<double> ReadStepsCountAsync()
{
var hasPermission = await health.CheckPermissionAsync(Health.Enums.HealthParameter.StepCount, Health.Enums.PermissionType.Read | Health.Enums.PermissionType.Write);
if (hasPermission)
{
return await health.ReadCountAsync(Enums.HealthParameter.StepCount, DateTime.Now.AddDays(-1), DateTime.Now);
}
}
}
Once you have registered the Health
plugin you can interact with it in the following ways:
IsSupported
Gets a value indicating whether reading the Health is supported on this device.
Sample
Represents a health-related sample, containing information such as time range, value, source, unit and description.
CheckPermissionAsync
Checks and requests the specified permissions for a given health parameter.ReadCountAsync
Reads the cumulative count of a specified "HealthParameter" within a given date range.ReadLatestAsync
Reads the latest health data value for a specified "HealthParameter" within a given date range.ReadLatestAvailableAsync
Reads the latest health data available value for a specified "HealthParameter" within a given date range and returns aSample
object.ReadAverageAsync
Reads the average value of a specified "HealthParameter" within a given date range.ReadMinAsync
Reads the min value of a specified "HealthParameter" within a given date range.ReadMaxAsync
Reads the max value of a specified "HealthParameter" within a given date range.ReadAllAsync
Reads all health data for a specified "HealthParameter" within a given date range and returns them as a list ofSample
objects.WriteAsync
Writes a value for a specified "HealthParameter" to the HealthKit store.
// check if user granted permission for reading and writing the step count
var hasPermission = await health.CheckPermissionAsync(Health.Enums.HealthParameter.StepCount, Health.Enums.PermissionType.Write);
// reading the steps count from the health kit
var hasPermission = await health.CheckPermissionAsync(Health.Enums.HealthParameter.StepCount, Health.Enums.PermissionType.Write);
if (hasPermission)
{
var stepsCount = await health.ReadCountAsync(Enums.HealthParameter.StepCount, DateTime.Now.AddDays(-1), DateTime.Now);
}
// writing the body mass into the health kit
var hasPermission = await Health.CheckPermissionAsync(HealthParameter.BodyMass, PermissionType.Read | PermissionType.Write);
if (hasPermission)
{
await Health.WriteAsync(HealthParameter.BodyMass, DateTime.Now, NewBodyMass, Units.Mass.Kilograms);
}
Try the sample app to test the plugin by your own.
When using the Plugin, make sure that you pass the correct HealthParameter with the corresponding unit to the methods. There is a utility class called Constants.Units that contains the supported units.
This project could not have came to be without these projects and people, thank you! <3
- We thank Gerald Versluis (@jfversluis) for his excellent template Plugin.Maui.Template