0

I follow the tutorial here (Code from Git Code repo here) for Xamarin building the first ios health-kit app but the permission dialog is not shown in the user's end. Both keywords NSHealthShareUsageDescription and NSHealthUpdateUsageDescription are in the info.plist. The code asking permission is

private void ValidateAuthorization ()
        {
            //Request / Validate that the app has permission to store heart-rate data
            var heartRateId = HKQuantityTypeIdentifierKey.HeartRate;
            var heartRateType = HKObjectType.GetQuantityType (heartRateId);
            var typesToWrite = new NSSet (new [] { heartRateType });
            //We aren't reading any data for this sample
            var typesToRead = new NSSet ();
            healthKitStore.RequestAuthorizationToShare (
                typesToWrite, 
                typesToRead, 
                ReactToHealthCarePermissions);
        }

If I run the app on iOS simulator, the output reports "Missing com.apple.developer.healthkit entitlement" error. I have enabled the HealthKit permission in the Entitlements.plist file and selected the option accordingly.

HKWork[935:10236] [default] connection error: Error Domain=com.apple.healthkit Code=4 "Missing com.apple.developer.healthkit entitlement." UserInfo={NSLocalizedDescription=Missing com.apple.developer.healthkit entitlement.} Thread started: #2 HKWork[935:10236] [auth] Failed to determine authorization status: Error Domain=com.apple.healthkit Code=4 "Missing com.apple.developer.healthkit entitlement." UserInfo={NSLocalizedDescription=Missing com.apple.developer.healthkit entitlement.} HKWork[935:10241] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x60000167c9a0> F8BB1C28-BAE8-11D6-9C31-00039315CD46

Any amount of help would be appreciated.

universe123
  • 103
  • 5
  • 1
    For projects created using Xcode 13 or later, set NSHealthShareUsageDescription and NSHealthUpdateUsageDescription in the Target Properties list on the app’s Info tab. For projects created with Xcode 12 or earlier, set these keys in the apps Info.plist file. Also,app must be properly configured and provisioned so it can use healthkit api. – Adrain Jul 05 '22 at 01:57
  • I am using Visual Studio 2022 and Xcode 13. In Visual Studio, I wasn't able to find the Target Properties list that you mentioned. Do you know where I can set it up? – universe123 Jul 05 '22 at 02:06
  • 1
    In xcode: Project -> Targets -> Info -> Custom iOS Target Properties. – Adrain Jul 05 '22 at 02:20
  • I checked, both keywords are set up in target properties list. The provisioning profile on Apple Developer website has been enabled HealthKit Capabilities. – universe123 Jul 05 '22 at 03:12
  • 1
    Are you sure you are actually using that specific provisioning profile? Seems like the entitlements are missing. – Cheesebaron Jul 05 '22 at 05:54
  • Yeah, in Visual Studio 2022, under ProjectOptions->iOS Bundle Signing, I can see that provisioning profile is selected and the provisioning profile has enabled healthkit on Apple developer website. – universe123 Jul 05 '22 at 15:44
  • 1
    you can check this issue on github https://github.com/xamarin/xamarin-macios/issues/8430 – Adrain Jul 06 '22 at 06:47
  • Thank you for your help! I found my problem is: at Project Options->iOS Bundle signing, I need to configure both the platform of iPhoneSimulator and the platform of iPhone (select the dropdown menu to configure both). Both the signing identity and provisioning profile can't be automatic. The custom Entitlements includes Entitlemets.plist. I didn't set under the platform of iPhone. This change enables poping up permission page when simulating on iPhone, but not on the simulator. I don't know why. (But when running the code in the link Adrain gave above, permission page pop up in simulator) – universe123 Jul 06 '22 at 17:03
  • 1
    you can post your own answer so it can help others – Adrain Jul 11 '22 at 01:25

1 Answers1

0

I found my problem is: at Project Options->iOS Bundle signing, I need to configure both the platform of iPhoneSimulator and the platform of iPhone (select the dropdown menu to configure both). Both the signing identity and provisioning profile can't be automatic. The custom Entitlements needs to include Entitlemets.plist. I didn't set under the platform of iPhone. This change enables poping up permission page when simulating on iPhone, but not on the simulator.

Meanwhile, in Entitlement.pList, enabling capability of HealthKit might change the value of the key "com.apple.developer.healthkit". This might be a bug in Visual Studio and just don't manually change the value back because that will disable the healthkit capability.

universe123
  • 103
  • 5
  • 1
    What worked for me was selecting manual provisioning for IOS, clicking on Bundle Signing Options and making sure that Custom Entitlements was set to Entitlements.plist. Before doing this, I could never get the permissions dialog to show. – Flood Techs Oct 07 '22 at 23:41