I am trying to integrate HealthKit with an app I'm building, and I seem to have successfully added the entitlement for it. I've created and downloaded a provisioning profile with the appropriate app ID, and the HealthKit toggle in the Xcode Capabilities tab seems to look fine:
However, when I run the app, I get the following error:
HealthKit authorization denied!
Error Domain=com.apple.healthkit Code=4 "Missing com.apple.developer.healthkit entitlement." UserInfo={NSLocalizedDescription=Missing com.apple.developer.healthkit entitlement.}
This is a strange error message to be getting since Add the "HealthKit" entitlement to your App ID
is already checked off as successfully completed.
However, once I get that error, the checked App ID entitlement changes to:
But in my App ID in my developer account, the App ID shows that HealthKit is indeed enabled.
And it shows the same thing in my provisioning profile.
And I only have one App ID, and one provisioning profile to choose from, so it's impossible I'm getting them mixed up.
I have a very similar problem with CloudKit entitlements now :( Works in Debug in Simulator and device, but not when built using distribution profile, entitlements just don't end up in the final archive. Passes validation though... odd.
Did you by any chance solve this?
If not, there are a few things you could still check.
To make sure that your Xcode is not confused about which profile to use - go to Preferences -> Accounts -> View Details -> Right click on one of the profiles -> Show in Finder. Now delete all of the profiles in that folder. We are doing this so that Xcode can't accidentally use the wrong profile.
To make sure that Xcode can find the most up to date profile, I would regenerate the profile again just in case (delete and re-add) with a new name. Then download and double click. (Downloading through Xcode can be flaky a times)
At this point it's worth checking that the provisioning profile does in fact contain the right entitlement keys. You can run 'security cms -D -i profile_name.mobileprovision' and check entitlements section. It should contain 'com.apple.developer.healthkit' key
Now you can go to Xcode and explicitly set code signing identity (will be something like 'iPhone Developer: ...' or 'iPhone Distribution: ...' - depending on how you are doing your testing). After that you can explicitly set the Provisioning profile - Xcode should be able to find it and show you the name of the profile - it should match the name you used in step 2. Make sure you set things correctly for Debug/Release (again depends how you are testing). If Xcode complains that it cannot find profile with specific id -> go to that folder where profiles were and make sure it's there, the filename will start with the id, then just restart Xcode, it should work after that.
If you are building actual archives or ipas, it would be good to know what entitlements end up inside those packages (you can open them up and check; for archive: archive file (right click, show package contents) -> Products -> Applications -> app name.app (right click, show package contents) -> archived-expanded-entitlements.xcent (text file); for ipa (similarly): rename to zip -> unzip -> Payload -> app name.app -> archived-expanded-entitlements.cent
----- EDIT ------
I've fixed my problem. I think my project file was in a weird state because I renamed things at some point in the past and had to update some entries manually in the project file.
I knew which step of the build process was going wrong. It was the ProcessProductPackaging step that took original entitlements and generated the .xcent file that was eventually used for signing.
For some reason, for Release configuration, Xcode was not honouring the location of my entitlements file, instead it just used the default template from the SDK, eg. /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.2.sdk/Entitlements.plist
I couldn't figure out why exactly, but things seemed to work ok for Debug configuration. So I nuked my Release config and created a new one with a different name, after that the build process started to pick up my entitlements file. From the project file diff it still wasn't obvious why things didn't work before.
In my case I had HealthKit integrated for a while, everything worked as it should have, and one day it just stopped working and it looked similar to your case.
After trying any straight forward / out of the box solution I was able to find online, my search was finally ended when I saw this answer:
Simply restarting my iPhone fixed the issue!
Hope it'll fix yours as well. Good luck
来源:https://stackoverflow.com/questions/33071726/missing-healthkit-entitlement