ios - HealthKit 许可表未出现

标签 ios swift watchkit healthkit

在我的 watch 扩展中,我调用了这个函数:

func requestAuthorization() {
        let healthStore = HKHealthStore()
        let workoutType = HKObjectType.workoutType()
        let heartRateType = HKObjectType.quantityType(forIdentifier: .heartRate)

        //reading
        let readingTypes = Set([heartRateType!, workoutType])

        //writing
        let writingTypes = Set([heartRateType!, workoutType])

        //auth request
        healthStore.requestAuthorization(toShare: writingTypes, read: readingTypes) { (success, error) -> Void in

            if error != nil {
                print("error \(error?.localizedDescription)")
            } else if success {
                self.startButton.setEnabled(true)
            } else if !success {
                self.startButton.setEnabled(false)
            }
        }
    }

在 AppDelegate.swift 中,我有:

func applicationShouldRequestHealthAuthorization(_ application: UIApplication) {
        let healthStore = HKHealthStore()
        healthStore.handleAuthorizationForExtension { (success, error) -> Void in
            //...
        }
    }

我在 watch 和手机上看到了对话框,当我从对话框中告诉它时,它会在手机上打开应用程序。我遇到的问题是电话应用程序没有显示应该显示以允许权限的权限表。此处提到了许可表: https://developer.apple.com/reference/healthkit

我需要做什么才能让它出现以便授予权限?就目前而言,我可以通过转到“健康”应用程序然后获取并选择我的应用程序并以这种方式授予权限来授予权限。

我说的许可表就是这个。 enter image description here

编辑:我从 requestAuthorization() 方法调用中打印出此错误。

error Optional("Required usage description strings not present in app\'s Info.plist")

最佳答案

有了这个TestHealthKit ,我能够打开 health-kit 许可表。

        // 4.  Request HealthKit authorization
    (HealthStore as! HKHealthStore).requestAuthorizationToShareTypes(nil, readTypes: dataTypesToRead()) { (success, error) -> Void in

        //            self.CheckAuthorizationStatusFor(self.dataTypesToRead())
        if (success)
        {

            SuccessCompletion(success: success)
            return;
        }
        else
        {

            FailureCompletion(err: error)
            return;
        }

    }

此外,请检查您是否启用了如图所示的权利。 entitlements .

关于ios - HealthKit 许可表未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40777457/

相关文章:

swift - 在 Swift 中实现可失败初始化器的最佳实践

objective-c - Apple Watch (WatchKit) 推送操作

ios - 将 CLLocationCoordinate2D 返回到结构

ios - 如何将角标(Badge)添加到 UINavigationItem (UIBarButtonItem) - Swift 3

ios - 无法隐藏 ViewController 的状态栏嵌入在 NavigationController 中

ios - WKWatchConnectivityRefreshBackgroundTask 与 WCSessionDelegate 竞争

ios - WatchKit 中基于 rowindex 的 prepareForSegue 等价物

ios - 在 obj-c 中调用类方法时出错

ios - 在 swift 中从 firebase 检索数组元素

ios - UILabel 未显示在屏幕截图中