[英]Apple HealthKit authorization not working (Xcode 13.3) - FAILED prompting authorization request
I have a problem with Apple HealthKit authorization.我对 Apple HealthKit 授权有疑问。 Everything worked fine until update of Xcode to version 13.3.
一切正常,直到将 Xcode 更新到 13.3 版。 It seems that that request for authorization is not fired, even when I explicitly declared that I want to request authorization onAppear of ContentView.
即使我明确声明要在 ContentView 的 Appear 上请求授权,似乎也没有触发授权请求。 This is code for
ContentView
:这是
ContentView
的代码:
import SwiftUI
struct ContentView: View {
@EnvironmentObject var firebaseManager: FirebaseManager
@EnvironmentObject var healthkitManager: HealthKitManager
var body: some View {
NavigationView {
if firebaseManager.signedIn {
HomePageView()
} else {
SignInView()
}
}
.onAppear {
healthkitManager.authorizeHealthKit()
firebaseManager.signedIn = firebaseManager.isSignedIn }
}
}
Function in HealthKitManager
looks like this: HealthKitManager
中的函数如下所示:
func authorizeHealthKit() {
//Check to see if HealthKit Is Available on this device
guard HKHealthStore.isHealthDataAvailable() else {
print("HealthKit data not available on this device")
return
}
// Set types to read and write in HealthStore
let typesToRead: Set = [
HKObjectType.characteristicType(forIdentifier: .dateOfBirth)!,
HKObjectType.quantityType(forIdentifier: .bloodGlucose)!,
HKObjectType.quantityType(forIdentifier: .insulinDelivery)!,
HKObjectType.quantityType(forIdentifier: .dietaryCarbohydrates)!,
HKObjectType.quantityType(forIdentifier: .stepCount)!,
HKObjectType.quantityType(forIdentifier: .heartRate)!,
HKObjectType.quantityType(forIdentifier: .appleExerciseTime)!,
]
let typesToWrite: Set = [
HKObjectType.quantityType(forIdentifier: .bloodGlucose)!,
HKObjectType.quantityType(forIdentifier: .insulinDelivery)!,
HKObjectType.quantityType(forIdentifier: .dietaryCarbohydrates)!,
]
// Request authorization for those quantity types.
healthStore.requestAuthorization(toShare: typesToWrite, read: typesToRead) { (success, error) in }
}
I've tried to add key Privacy - Health Update Usage Description
and Privacy - Health Share Usage Description
with some string values to Info
tab in project file, but still nothing.我尝试将关键
Privacy - Health Update Usage Description
和Privacy - Health Share Usage Description
与一些字符串值添加到项目文件中的Info
选项卡,但仍然没有。 When I build application, I get this in console:当我构建应用程序时,我在控制台中得到了这个:
[auth] FAILED prompting authorization request to share (
HKQuantityTypeIdentifierBloodGlucose,
HKQuantityTypeIdentifierDietaryCarbohydrates,
HKQuantityTypeIdentifierInsulinDelivery
), read (
HKCharacteristicTypeIdentifierDateOfBirth,
HKQuantityTypeIdentifierHeartRate,
HKQuantityTypeIdentifierBloodGlucose,
HKQuantityTypeIdentifierInsulinDelivery,
HKQuantityTypeIdentifierDietaryCarbohydrates,
HKQuantityTypeIdentifierAppleExerciseTime,
HKQuantityTypeIdentifierStepCount
)
I read some articles, tried multiple possible solutions, restarted my Mac, but everything without success.我阅读了一些文章,尝试了多种可能的解决方案,重新启动了我的 Mac,但一切都没有成功。 Should there be a problem because I have two environment object?
因为我有两个环境对象应该有问题吗? I'll be thankful for any ideas...
我会感谢任何想法...
you have to add HealthKit capability to your target您必须将 HealthKit 功能添加到您的目标
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.