Apple HealthKit 授权不起作用 (Xcode 13.3) - 提示授权请求失败

发布于 2025-01-14 16:54:38 字数 2634 浏览 4 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红焚 2025-01-21 16:54:38

您必须将 HealthKit 功能添加到您的目标

  • 在 Xcode 中选择您的项目
  • 在 Xcode 中选择您的目标
  • 选择签名和功能
  • 点击 + 功能
  • 搜索 HealthKit
  • 将其添加到您的项目

you have to add HealthKit capability to your target

  • select your project in xcode
  • select your target in xcode
  • select signing and capabilities
  • tap on + Capability
  • search for HealthKit
  • add it to your project
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文