Skip to main content
MonoAndroid13.0Xamarin.iOS1.0net8.0net8.0-android34.0net8.0-ios17.0.NETStandard2.1

Cross Platform health data - Apple Health and Google Fit Integrations

Published
Nov 27, 2023
2y ago
Total Downloads
4.8K
4,843

Install

dotnet add package Shiny.Health --version 1.0.0-beta-0018
ProductVersions
.NET
net8.0net8.0-android34.0net8.0-ios17.0
.NET Standard
netstandard2.1
MonoAndroid
monoandroid13.0
Other
Xamarin.iOS1.0

README

Shiny Health

Apple Health and Google Fit for your .NET7+ .NET Mobile apps

Features

  • Read summary values between timestamps and specified interval
  • Query distance, step count, calory intake, & heart rate
  • TODO: Write values

How To Use

IHealthService health; // inject, resolve, etc

// request permissions
var result = await health.RequestPermission(
    new Permission(DistanceHealthMetric.Default, PermissionType.Read),
    new Permission(CaloriesHealthMetric.Default, PermissionType.Read),
    new Permission(StepCountHealthMetric.Default, PermissionType.Read),
    new Permission(HeartRateHealthMetric.Default, PermissionType.Read)
);
if (!result)
{
    // say something useful
}

var end = DateTimeOffset.Now;
var start = DateTimeOffset.Now.AddDays(-1);

// now run your queries
var distance = (await health.Query(DistanceHealthMetric.Default, start, end, Interval.Days)).Sum(x => x.Value);
var calories = (await health.Query(CaloriesHealthMetric.Default, start, end, Interval.Days)).Sum(x => x.Value);
var steps = (await health.Query(StepCountHealthMetric.Default, start, end, Interval.Days)).Sum(x => x.Value);
var heartRate = (await health.Query(HeartRateHealthMetric.Default, start, end, Interval.Days)).Average(x => x.Value);

Setup

The fastest way to get going in a new application is to use our community dotnet template. Just run

dotnet new install Shiny.Templates

MAUI

Install Shiny.Health from NuGet

Now, in your MauiProgram.cs, add:


public static MauiApp CreateMauiApp()
{
    var builder = MauiApp
        .CreateBuilder()
        .UseShiny()
        .UseMauiApp<App>()
        .ConfigureFonts(fonts =>
        {
            fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); 
        });

    builder.Services.AddHealthIntegration();
    return builder.Build();
}

iOS

To use iOS (and therefore the sample), you need to have a provisioning profile with all of the necessary Apple Health setup for your application.

Take a look at the Xamarin docs for more info

Add the following:

Info.plist
<key>UIRequiredDeviceCapabilities</key>
<array>
        <string>healthkithealthkit</string>
</array>
<key>NSHealthUpdateUsageDescription</key>
<string>We need to say something useful here</string>
<key>NSHealthShareUsageDescription</key>
<string>We need to say something useful here</string>
Entitlements.plist
<key>com.apple.developer.healthkit</key>
<true />
<key>com.apple.developer.healthkit.background-delivery</key>
<true />
Google Fit

Google Fit

To Test Locally:

  • You have added your SHA-1 debug key to your Project's configuration setting on Google or Firebase Account.
  • Downloaded and added google-services.json to your android project after adding the debug SHA1 key in your account.
  • Package name (on Firebase account) and Application Id (on android) must be same .. Android's package name might be different, no problems with that.

** https://github.com/android/fit-samples/blob/main/StepCounterKotlin/app/src/main/java/com/google/android/gms/fit/samples/stepcounterkotlin/MainActivity.kt ** https://github.com/android/fit-samples/blob/main/BasicHistoryApiKotlin/app/src/main/AndroidManifest.xml

AndroidManifest.xml

The following entry is needed for step data

<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>

Changes from Previous Version

v1.0.0-beta-0013v1.0.0-beta-0018
Added Frameworks
MonoAndroid13.0net8.0net8.0-android34.0net8.0-ios17.0
Removed Frameworks
MonoAndroid12.0net7.0net7.0-android33.0net7.0-ios16.1
Added Dependencies
Shiny.Core(MonoAndroid13.0)
Shiny.Core(net8.0)
Shiny.Core(net8.0-android34.0)
Shiny.Core(net8.0-ios17.0)
Removed Dependencies
Shiny.Core(MonoAndroid12.0)
Shiny.Core(net7.0)
Shiny.Core(net7.0-android33.0)
Shiny.Core(net7.0-ios16.1)

Adoption Assessment

A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.

This version is marked prerelease. Validate its API and upgrade path before production use.Compatibility metadata lists 6 target frameworks; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.