Apple HealthKit 授权不起作用 (Xcode 13.3) - 提示授权请求失败
我的 Apple HealthKit 授权有问题。一切工作正常,直到 Xcode 更新到版本 13.3。即使我明确声明我想在 ContentView 的 Appear 上请求授权,授权请求似乎也不会被触发。这是 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 }
}
}
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 }
}
我尝试添加密钥隐私 - 健康更新使用说明
和 隐私 - 健康共享使用说明
在项目文件中的Info
选项卡中包含一些字符串值,但仍然没有任何内容。当我构建应用程序时,我在控制台中得到以下信息:
[auth] FAILED prompting authorization request to share (
HKQuantityTypeIdentifierBloodGlucose,
HKQuantityTypeIdentifierDietaryCarbohydrates,
HKQuantityTypeIdentifierInsulinDelivery
), read (
HKCharacteristicTypeIdentifierDateOfBirth,
HKQuantityTypeIdentifierHeartRate,
HKQuantityTypeIdentifierBloodGlucose,
HKQuantityTypeIdentifierInsulinDelivery,
HKQuantityTypeIdentifierDietaryCarbohydrates,
HKQuantityTypeIdentifierAppleExerciseTime,
HKQuantityTypeIdentifierStepCount
)
我阅读了一些文章,尝试了多种可能的解决方案,重新启动了我的 Mac,但一切都没有成功。因为我有两个环境对象,应该有问题吗?我会很感激任何想法......
I have a problem with Apple HealthKit authorization. Everything worked fine until update of Xcode to version 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. This is code for 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:
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. 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. Should there be a problem because I have two environment object? I'll be thankful for any ideas...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将 HealthKit 功能添加到您的目标
you have to add HealthKit capability to your target