Skip to content

0xc3u/Plugin.Maui.Health

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Plugin.Maui.Health

Plugin.Maui.Health provides the ability to access personal health data in your .NET MAUI application.

Build Status

ci

Install Plugin

NuGet

Available on NuGet.

Install with the dotnet CLI: dotnet add package Plugin.Maui.Health, or through the NuGet Package Manager in Visual Studio.

API Usage

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 supported

Platform Minimum Version Supported
iOS 14.0+
Android currently not supported

Permissions

Before you can start using Health, you will need to request the proper permissions on each platform.

iOS

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>

Dependency Injection

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;
    }
}

Straight usage

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);
			}
    }
}

Health Plugin

Once you have registered the Health plugin you can interact with it in the following ways:

Properties

  • IsSupported Gets a value indicating whether reading the Health is supported on this device.

Models

  • Sample Represents a health-related sample, containing information such as time range, value, source, unit and description.

Methods

  • 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 a Sample 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 of Sample objects.
  • WriteAsync Writes a value for a specified "HealthParameter" to the HealthKit store.

Examples

Checking Permissions
	// 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 a value
	// 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 a value
	// 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);
	}

Sample App

Screenshot of the sample app - Dashboard Screenshot of the sample app - Vitamins Screenshot of the sample app - Body Measurements Screenshot of the permission granted to the sample app

Try the sample app to test the plugin by your own.

Remarks

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.

Acknowledgements

This project could not have came to be without these projects and people, thank you! <3