I'm creating a new multiplatform app using the Xcode beta run on macOS Ventura beta. I want to use HealthKit to submit mindfulness minutes but I can't get it working. I created a new app with JUST this and I have the same issue so I came here hoping someone knows how to fix it.
The ContentView for the app is:
import SwiftUI
import HealthKit
struct ContentView: View {
// You need only a single HealthKit store per app. These are long-lived objects; you create the store once, and keep a reference for later use.
let myHealthStore = HKHealthStore()
let typestoRead = Set([HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.mindfulSession)!])
let mindfulType = HKObjectType.categoryType(forIdentifier: .mindfulSession)
@State var message: String?
var body: some View {
NavigationView {
if HKHealthStore.isHealthDataAvailable() {
Text("Health Data Available")
} else {
Text("Health Data NOT Available")
}
Button(action: {
print("Setting 1 minute Mindfulness")
let startTime = Date()
let endTime = startTime.addingTimeInterval(2.0 * 60.0)
print("From \(startTime) until \(endTime).")
// Create a mindful session with the given start and end time
let mindfullSample = HKCategorySample(type:mindfulType!, value: 0, start: startTime, end: endTime)
// Save it to the health store
myHealthStore.save(mindfullSample, withCompletion: { (success, error) -> Void in
print("in completion")
if error != nil {
print("error: \(error!)")
message = "Failure: \(error!)"
return
}
print("New data was saved in HealthKit: \(success)")
message = "Success: \(success)"
})
}, label: {
Text("Set 1 Minute Mindfulness")
})
.buttonStyle(.bordered)
Divider()
Text(message ?? "No Message.... yet")
}
.onAppear() {
print("ContentView appears")
myHealthStore.requestAuthorization(toShare: [], read: typestoRead) { (success, error) -> Void in
if(success){
// Read or write the HealthKit data
print("success: \(success)")
}
else{
// Authorization failure
print("error: \(error!)")
}
}
}
}
}
Additionally I went to Application, Target, Info, and pressed the plus sign to add the following PLIST values:
I also went to Application > Target > Signing & Capabilities > and pressed the plus to add HealthKit and check off Background Delivery
.
This got me past the first error but now when I run it I either get an error code 1 or 4. One is understandable, when run on my iPad or Mac as apparently Health data is unavailable on this device
but, when run on the iOS simulator I don't understand how to get past the error:
Error Domain=com.apple.healthkit Code=4 "Missing application-identifier entitlement" UserInfo={NSLocalizedDescription=Missing application-identifier entitlement}
What am I missing? I looked online but most people with this error seem to be updating an app. This is a newly created app. Just in case I've uninstalled it and reinstalled it on the simulator, chose a different simulator, and created a new bare bones app. Any tips to access HealthKit?
Please confirm that there is a bundle id string listed in the entitlements plist for the application-identifier
key? Error Code 4 is referring to that missing.