I'm adding code for HealthKit in my iOS Swift app, but I'm getting an error:
/* Ask for permission to access the health store */
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if HKHealthStore.isHealthDataAvailable(){
healthStore.requestAuthorizationToShareTypes(typesToShare,
readTypes: typesToRead,
completion: {(succeeded: Bool, error: NSError!) in
if succeeded && error == nil{
println("Successfully received authorization")
} else {
if let theError = error{
println("Error occurred = \(theError)")
}
}
})
} else {
println("Health data is not available")
}
}
Error occurred = Error Domain=com.apple.healthkit Code=4 "Missing com.apple.developer.healthkit entitlement." UserInfo=0x7fa748534b00 {NSLocalizedDescription=Missing com.apple.developer.healthkit entitlement.}
How can solve this?
To add this entitlement to your app, first enable the HealthKit capability in Xcode, and then check any values that you want to add to the HealthKit Capabilities Entitlement. Only add values for data types that your app needs to access. App Review may reject apps that don't use the data appropriately.
Enable HealthKitIn Xcode, select the project and add the HealthKit capability. Only select the Clinical Health Records checkbox if your app needs to access the user's clinical records. App Review may reject apps that enable the Clinical Health Records capability if the app doesn't actually use the health record data.
You'll tap "Signing & Capabilities" and hit that "+ Capability" button. You'll type in "HealthKit" and right then and there you'll add the HealthKit framework into your application, which will allow you to retrieve and store users' health information.
Before you can use HealthKit, you must enable the HealthKit capabilities for your app. In Xcode, select the project and add the HealthKit capability. Only select the Clinical Health Records checkbox if your app needs to access the user’s clinical records.
With the user’s permission, apps communicate with the HealthKit store to access and share this data. Creating a complete, personalized health and fitness experience includes a variety of tasks:
If HealthKit isn’t required for the correct operation of your app, delete the healthkit entry from the Required device capabilities array. For projects created using Xcode 13 or later, delete this entry from the Target Properties list on the app’s Info tab. For projects created with Xcode 12 or earlier, delete it from the apps Info.plist file.
If HealthKit isn’t available on the device (for example, on an iPad), other HealthKit methods fail with an errorHealthDataUnavailable error. If HealthKit is restricted (for example, in an enterprise environment), the methods fail with an errorHealthDataRestricted error.
I spent 3 days trying to figure out what was the problem. I found the solution with those steps:
I am using iOS 9.3.2 and Xcode 7.3.1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With