如何使用healthkit解决此错误?

use*_*367 4 swift xcode6 ios8 healthkit

我在我的ios Swift应用程序中添加了healthkit的代码,但是我收到了错误...

 /* Ask for permission to access the health store */
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

if HKHealthStore.isHealthDataAvailable(){

  healthStore.requestAuthorizationToShareTypes(typesToShare,
    readTypes: typesToRead,
    completion: {(succeeded: Bool, error: NSError!) in

      if succeeded && error == nil{
        println("Successfully received authorization")
      } else {
        if let theError = error{
          println("Error occurred = \(theError)")
        }
      }

    })

} else {
  println("Health data is not available")
}

}
Run Code Online (Sandbox Code Playgroud)

发生错误=错误域= com.apple.healthkit代码= 4"缺少com.apple.developer.healthkit权利." UserInfo = 0x7fa748534b00 {NSLocalizedDescription =缺少com.apple.developer.healthkit权利.}

怎么解决这个?

谢谢!

Jor*_*nez 16

我花了3天时间试图找出问题所在.我找到了这些步骤的解决方案:

在此输入图像描述

  1. 首先去项目(正如我在图片中所示)
  2. 打开项目和目标列表
  3. 选择nameApp WatchKit Extension
  4. 转到功能选项卡,按健康工具包中的按钮,然后瞧...它对我有用.

我使用的是iOS 9.3.2和Xcode 7.3.1

  • Xcode-12.0.1 iOS-14、watchOS-7:一切都是一样的,除了“功能”选项卡称为“签名和功能”,并且您必须通过按右上角的“+”按钮添加 Health Kit 功能 (3认同)
  • 祝福你先生<3 (2认同)

小智 10

应首先尝试上述两个答案.但是如果

  1. 您确定该功能已被激活
  2. 您确信与您的配置文件相关联的应用ID已启用healthkit并与应用的捆绑ID相匹配

然后尝试通过选择目标并转到,强制构建使用该特定的配置文件

构建设置 - >代码签名 - >配置文件

并手动选择它.

如果使用"自动选择",我遇到了在调试中遇到上述错误的情况


jal*_*200 9

该错误是由于您尚未将HealthKit权利添加到目标.

1)转到Xcode中的项目设置.2)从那里,导航到"功能"选项卡.3)向下滚动,直到看到"HealthKit"并将开关拨到打开状态.4)Xcode现在将为您的Info.plist,您的{ProjectName} .entitlements文件和iTunes Connect上的App ID添加HealthKit权利

完成以下步骤后,请尝试再次运行您的应用.这一次它应该工作.


iOS*_*dev 5

在 Capabilities 中激活 Health kit

在此处输入图片说明

如果还是不行

check

  • 您的配置文件是否包含对 HealthKit 的支持?
  • 您的捆绑标识符是否使用与支持 HealthKit 的配置文件中引用的相同 ID?