简体   繁体   English

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

[英]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 DescriptionPrivacy - 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 功能添加到您的目标

  • select your project in xcode在 xcode 中选择您的项目
  • select your target in xcode在 xcode 中选择你的目标
  • select signing and capabilities选择签名和功能
  • tap on + Capability点击 + 能力
  • search for HealthKit搜索 HealthKit
  • add it to your project将其添加到您的项目中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HealthKit 请求授权失败 - HealthKit request authorization failing 已授予HealthKit授权并可以在模拟器上工作,但不能在实际设备上工作 - HealthKit authorization granted and working on simulator but not on actual device 如何撤销HealthKit授权 - How to revoke HealthKit Authorization 有没有办法使用 HealthKit 在 iOS 上请求对非临床和临床记录的授权? - Is there a way to request authorization for both nonclinical and clinical records on iOS with HealthKit? 位置授权未提示 - Location Authorization not prompting Xcode 8 iOS Simulator不提示我的Swift 3项目中的位置服务授权 - Xcode 8 iOS Simulator is not prompting for location services authorization in my Swift 3 project Apple Health Kit错误Domain = com.apple.healthkit代码= 5“未确定授权” - Apple Health Kit Error Domain=com.apple.healthkit Code=5 “Authorization not determined” 在HealthKit中请求授权:为什么结果总是成功? - Requesting authorization in HealthKit: why the result is always successful? IOS 8 Map无法正常工作:尝试启动MapKit位置更新而未提示位置授权 - IOS 8 Map is not working: Trying to start MapKit location updates without prompting for location authorization HealthKit requestAuthorization返回代码100:“授权会话超时” - HealthKit requestAuthorization returns code 100: “Authorization session timed out”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM