Xamarin-macios: Unable to access Healthkit data

Created on 19 Apr 2020  路  11Comments  路  Source: xamarin/xamarin-macios

Steps to Reproduce

  1. Create a new Xamarin Form application
  2. Set an explicit app bundle
  3. Enable HealthKit Entitlement

Expected Behavior

Prompt user to share Healthkit Data.

Actual Behavior

Get an Error 4.

Certificate is OK on apple, and it works very well on native swift application.

Screenshot 2020-04-19 at 03 06 34

2020-04-19 02:50:33.294171+0200 Breath.iOS[39273:4170035] [default] containerAppExtensionEntitlementsWithCompletion: failed with error: Error Domain=com.apple.healthkit Code=4 "Missing com.apple.developer.healthkit entitlement." UserInfo={NSLocalizedDescription=Missing com.apple.developer.healthkit entitlement.}
2020-04-19 02:50:33.295681+0200 Breath.iOS[39273:4170035] [default] containerAppExtensionEntitlementsWithCompletion: failed with error: Error Domain=com.apple.healthkit Code=4 "Missing com.apple.developer.healthkit entitlement." UserInfo={NSLocalizedDescription=Missing com.apple.developer.healthkit entitlement.}
2020-04-19 02:50:33.296755+0200 Breath.iOS[39273:4170035] [auth] FAILED prompting authorization request to share (
), read (
    HKQuantityTypeIdentifierHeartRate
)
Thread started:  #5
2020-04-19 02:50:33.512307+0200 Breath.iOS[39273:4170035] [default] connection error: Error Domain=com.apple.healthkit Code=4 "Missing com.apple.developer.healthkit entitlement." UserInfo={NSLocalizedDescription=Missing com.apple.developer.healthkit entitlement.}

Environment

=== Visual Studio Community 2019 for Mac (Preview) ===

Version 8.6 Preview (8.6 build 4243)
Installation UUID: eebf9799-f09f-48ce-a012-a3bd21789e63
    GTK+ 2.24.23 (Raleigh theme)
    Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638)

    Package version: 610000101

=== Mono Framework MDK ===

Runtime:
    Mono 6.10.0.101 (2019-12/6f0bf73005d) (64-bit)
    Package version: 610000101

=== Roslyn (Language Service) ===

3.6.0-3.20209.3+34fad56309fc96f2af1171df6ea012cbf991c27d

=== NuGet ===

Version: 5.5.0.6473

=== .NET Core SDK ===

SDK: /usr/local/share/dotnet/sdk/5.0.100-preview.2.20176.6/Sdks
SDK Versions:
    5.0.100-preview.2.20176.6
    3.1.201
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/6.10.0/lib/mono/msbuild/Current/bin/Sdks

=== .NET Core Runtime ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
    5.0.0-preview.2.20160.6
    3.1.3
    2.1.17
    2.1.16

=== Xamarin.Profiler ===

Version: 1.6.12.29
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Updater ===

Version: 11

=== Apple Developer Tools ===

Xcode 11.4 (16134)
Build 11E146

=== Xamarin.Mac ===

Version: 6.18.1.31 (Visual Studio Community)
Hash: b3eedfed9
Branch: d16-6
Build date: 2020-04-09 10:22:36-0400

=== Xamarin.iOS ===

Version: 13.18.1.31 (Visual Studio Community)
Hash: b3eedfed9
Branch: d16-6
Build date: 2020-04-09 10:22:36-0400

=== Xamarin Designer ===

Version: 16.6.0.320
Hash: dcc2cd183
Branch: remotes/origin/d16-6
Build date: 2020-04-10 18:49:40 UTC

=== Xamarin.Android ===

Version: 10.3.0.74 (Visual Studio Community)
Commit: xamarin-android/d16-6/964ac42
Android SDK: /Users/laibulle/Library/Android/sdk
    Supported Android versions:
        7.0 (API level 24)
        8.1 (API level 27)

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 29.0.6
SDK Build Tools Version: 29.0.2

Build Information: 
Mono: 165f4b0
Java.Interop: xamarin/java.interop/d16-6@2cab35c
ProGuard: xamarin/proguard/master@905836d
SQLite: xamarin/sqlite/3.31.1@49232bc
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-6@bfb66f3

=== Microsoft Mobile OpenJDK ===

Java SDK: /Users/laibulle/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_1.8.0.25
1.8.0-25
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Android SDK Manager ===

Version: 16.6.0.46
Hash: f6bfa7b
Branch: remotes/origin/dev/joe/d16-6~2
Build date: 2020-04-09 20:14:24 UTC

=== Android Device Manager ===

Version: 16.6.0.75
Hash: 4509c53
Branch: remotes/origin/d16-6~6
Build date: 2020-04-09 20:14:50 UTC

=== Build Information ===

Release ID: 806004243
Git revision: dda82db2458288a91a58f44d24abe41cf0d25bb6
Build date: 2020-04-14 16:13:25-04
Build branch: release-8.6
Xamarin extensions: dda82db2458288a91a58f44d24abe41cf0d25bb6

=== Operating System ===

Mac OS X 10.15.4
Darwin 19.4.0 Darwin Kernel Version 19.4.0
    Wed Mar  4 22:28:40 PST 2020
    root:xnu-6153.101.6~15/RELEASE_X86_64 x86_64

=== Enabled user installed extensions ===

Xamarin.Forms HotReload extension 1.4.0

  1. Paste into the code block below (between ```)
    -->
 public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();
            LoadApplication(new App());

            var read = new NSSet(HKQuantityType.Create(HKQuantityTypeIdentifier.HeartRate));

            var healthstore = new HKHealthStore();
            healthstore.RequestAuthorizationToShare(new NSSet(), read, (f, error) =>
            {
                if (error != null)
                {
                    Console.WriteLine(@"{0} error", error);
                }
            });

            return base.FinishedLaunching(app, options);
        }

Build Logs

https://gist.github.com/laibulle/1696b6de32eb6fa16bfc8504186795f3

Example Project (If Possible)

https://github.com/laibulle/nixe.io

iOS need-info

All 11 comments

I'm having the exact same issue, were you able to find a workaround?

hello @Matthew-Pitzer my workaround is to use ReactNative for now and wait for Xamarin to fix this issue.

@laibulle Thanks for your response! I was able to get mine working, but the built in entitlements GUI picker in Visual Studio for Mac doesn't format the entitlements correctly.

@chamons I agree his entitlements file is formatted incorrectly as the entitlements for HealthKit should look like:
<key>com.apple.developer.healthkit</key>
<true/>

However when selecting HealthKit from the entitlements GUI Picker the raw entitlements.plist is set to this:
<key>com.apple.developer.healthkit</key>
<array/>

I believe the GUI is autofilling the file in an incorrect format which results in it not working.

My solution was to manually edit the entitlements.plist file in a text editor and add the correctly formatted entitlements through there. Also just be aware that once you set it correctly, the GUI no longer will have the HealthKit entitlement selected, but it will still work.

Good catch, there appears to be an IDE bug I can confirm.

I've filed it as internal bug 1108910.

@laibulle can you confirm that with a correct entitlement you are unblocked?

Thanks for your feed back.

@Matthew-Pitzer I already tried to manually edit the entitlements.plist few days ago and it did not fixed the issue. But I am happy to know that I have to edit it.

Here is my entitlements.plist

<?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>

@chamons I also tried to use the provisionning profile and bundle identifier that I use in my ReactNative implementation of the same app. But I have the exact same error.

I will create a new Xamarin application without XamarinForm and Watch Extension to see if I have the same issue.

@chamons I cloned your test project https://github.com/chamons/filed-bug-test-cases/tree/master/iOSHealth and it works as expected.

So I tried to on my side

1 - Create a new iOS project
2- Add the healthkit entitlement.plist
3 - Update the entitlement.plist to replace array with a boolean
4 - Add "Privacy - Health Share Usage Description" and "Privacy - Health Update Usage Description"
5 - Request permission in "FinishedLaunching" in AppDelegate.cs
6 - Run in simulator

get

2020-04-22 23:45:08.925185+0200 HealthTest[3081:86987] Xamarin.iOS: Established connection with the IDE (fd: 5)
2020-04-22 23:45:08.925534+0200 HealthTest[3081:86987] Xamarin.iOS: Processing: 'start profiler: no'
2020-04-22 23:45:08.925636+0200 HealthTest[3081:86906] Xamarin.iOS: Profiler not loaded (disabled)
2020-04-22 23:45:08.974082+0200 HealthTest[3081:86906] Xamarin.iOS: Added dllmap for objc_msgSend
Loaded assembly: /Users/laibulle/Library/Developer/CoreSimulator/Devices/C9E036E9-5859-4D61-8113-C3264833953C/data/Containers/Bundle/Application/19B82FF1-FECC-4652-975C-83F720E25325/HealthTest.app/Xamarin.iOS.dll [External]
Loaded assembly: /Users/laibulle/Library/Developer/CoreSimulator/Devices/C9E036E9-5859-4D61-8113-C3264833953C/data/Containers/Bundle/Application/19B82FF1-FECC-4652-975C-83F720E25325/HealthTest.app/System.dll [External]
Loaded assembly: /Users/laibulle/Library/Developer/CoreSimulator/Devices/C9E036E9-5859-4D61-8113-C3264833953C/data/Containers/Bundle/Application/19B82FF1-FECC-4652-975C-83F720E25325/HealthTest.app/Mono.Security.dll [External]
Loaded assembly: /Users/laibulle/Library/Developer/CoreSimulator/Devices/C9E036E9-5859-4D61-8113-C3264833953C/data/Containers/Bundle/Application/19B82FF1-FECC-4652-975C-83F720E25325/HealthTest.app/HealthTest.exe
2020-04-22 23:45:09.225398+0200 HealthTest[3081:86906] SecTaskLoadEntitlements failed error=22 cs_flags=200, pid=3081
2020-04-22 23:45:09.225697+0200 HealthTest[3081:86906] SecTaskCopyDebugDescription: HealthTest[3081]/0#-1 LF=0
2020-04-22 23:45:09.232042+0200 HealthTest[3081:86906] SecTaskLoadEntitlements failed error=22 cs_flags=200, pid=3081
2020-04-22 23:45:09.232264+0200 HealthTest[3081:86906] SecTaskCopyDebugDescription: HealthTest[3081]/0#-1 LF=0
2020-04-22 23:45:09.298296+0200 HealthTest[3081:86936] [default] containerAppExtensionEntitlementsWithCompletion: failed with error: Error Domain=com.apple.healthkit Code=4 "Missing com.apple.developer.healthkit entitlement." UserInfo={NSLocalizedDescription=Missing com.apple.developer.healthkit entitlement.}
2020-04-22 23:45:09.298738+0200 HealthTest[3081:86906] SecTaskLoadEntitlements failed error=22 cs_flags=200, pid=3081
2020-04-22 23:45:09.298971+0200 HealthTest[3081:86906] SecTaskCopyDebugDescription: HealthTest[3081]/0#-1 LF=0
2020-04-22 23:45:09.300224+0200 HealthTest[3081:86936] [default] containerAppExtensionEntitlementsWithCompletion: failed with error: Error Domain=com.apple.healthkit Code=4 "Missing com.apple.developer.healthkit entitlement." UserInfo={NSLocalizedDescription=Missing com.apple.developer.healthkit entitlement.}
2020-04-22 23:45:09.300470+0200 HealthTest[3081:86906] SecTaskLoadEntitlements failed error=22 cs_flags=200, pid=3081
2020-04-22 23:45:09.300640+0200 HealthTest[3081:86906] SecTaskCopyDebugDescription: HealthTest[3081]/0#-1 LF=0
2020-04-22 23:45:09.301760+0200 HealthTest[3081:87022] [auth] FAILED prompting authorization request to share (
), read (
    HKQuantityTypeIdentifierHeartRate
)
Thread started:  #2
Loaded assembly: /Users/laibulle/Library/Developer/CoreSimulator/Devices/C9E036E9-5859-4D61-8113-C3264833953C/data/Containers/Bundle/Application/19B82FF1-FECC-4652-975C-83F720E25325/HealthTest.app/System.Net.Http.dll [External]
2020-04-22 23:45:09.378415+0200 HealthTest[3081:87022] Missing com.apple.developer.healthkit entitlement. error

7 - Thinking about what I missed
8 - Cry a little bit
9 - push the code on https://github.com/laibulle/healthtest
10 - Write a comment on https://github.com/xamarin/xamarin-macios/issues/8430
11 - Pray for someone to find the stupid thing I missed
12 - Click on Comment butt..

See https://github.com/laibulle/healthtest/pull/1

This is an easy mistake. We had https://github.com/xamarin/xamarin-macios/issues/8102 filed to improve the situation.

Closing for now, as it appears to be project specific configuration.

@chamons, I come from ReactNative and I am new in the .Net ecosystem. Sometimes I am a bit confused about how Xamarin works.

Thanks a lot for your help.

what was the solution?

Was this page helpful?
0 / 5 - 0 ratings