Search code examples
.netmauivisual-studio-2022healthkitapple-developer

How we can access the Healthkit framework in .net MAUI app and access the HealthData


How can we get the data of Healthkit in .net MAUI application? We have this code to get healthdata.

But this code gives me error on HkHealthStore. So how we can access the healthstore in .net MAUI application.

Do we need to do the development only in IOS System or is it possible to do it in Windows system as well?

How can we start the Healthkit app in .net MAUI?

I am trying to make a .net MAUI app in which i can access the Healthkit framework. But there is no document or any suggestion over internet by which i can access the Healthkit framework.

I created a .net MAUI application and copy pasted the Healthdata.cs file but i am getting error on HKHealthStore.

The type or namespace name HKHealthStore could not be found (are you missing a using directive or an assembly reference)


Solution

  • I don't have enough information to determine the definitive error here, but I think I have a pretty good idea.

    For Xamarin the projects were all separate so you would always have the dedicated iOS context (or Android, or Windows, etc.) for .NET MAUI, you probably have a single-project, where it's not always clear what the context is that you are in.

    For a simple test you could try adding:

    #if IOS
        var foo = HealthKit.HKHealthStore.IsHealthDataAvailable;
    #endif
    

    This should work. If this becomes greyed out immediately, the IDE/editor you are using is looking at a different context (or build target).

    For Visual Studio go to the top-left of your text editor and switch it to iOS:

    The target platform switcher drop-down box in Visual Studio 2022

    Now the line referencing HealthKit should become active.

    From here, you should figure out how you use this platform-specific code from within your shared code as HealthKit is specific to iOS and won't be available on other platforms.

    For guidance on that, please check the official documentation here.