Swift &引用;“activeEnergyQuery”出现错误。错误是:未确定授权。”;

Swift &引用;“activeEnergyQuery”出现错误。错误是:未确定授权。”;,swift,healthkit,Swift,Healthkit,我正在使用苹果的健康套件演示应用程序ActivityRings。我已经正确设置了包标识符和权限。iOS应用程序和手表扩展正在工作,它的记录数据似乎还可以。应该没问题,因为我没有碰过任何代码 但是控制台日志显示,“activeEnergyQuery出现错误。错误是:未确定授权。” 正如您在报告查询和处理程序分配中所看到的,Apple为该错误编写了打印 我想知道这是干什么用的。是否有损坏的功能 // Create a query to report new Active Energy Burned

我正在使用苹果的健康套件演示应用程序ActivityRings。我已经正确设置了包标识符和权限。iOS应用程序和手表扩展正在工作,它的记录数据似乎还可以。应该没问题,因为我没有碰过任何代码

但是控制台日志显示,“activeEnergyQuery出现错误。错误是:未确定授权。”

正如您在报告查询和处理程序分配中所看到的,Apple为该错误编写了打印

我想知道这是干什么用的。是否有损坏的功能

// Create a query to report new Active Energy Burned samples to our app.
    let activeEnergyQuery = HKAnchoredObjectQuery(type: activeEnergyType, predicate: predicate, anchor: nil, limit: Int(HKObjectQueryNoLimit)) { query, samples, deletedObjects, anchor, error in
        if let error = error {
            print("An error occurred with the `activeEnergyQuery`. The error was: \(error.localizedDescription)")
            return
        }
        // NOTE: `deletedObjects` are not considered in the handler as there is no way to delete samples from the watch during a workout.
        guard let activeEnergySamples = samples as? [HKQuantitySample] else { return }
        sampleHandler(activeEnergySamples)
    }

    // Assign the same handler to process future samples generated while the query is still active.
    activeEnergyQuery.updateHandler = { query, samples, deletedObjects, anchor, error in
        if let error = error {
            print("An error occurred with the `activeEnergyQuery`. The error was: \(error.localizedDescription)")
            return
        }
        // NOTE: `deletedObjects` are not considered in the handler as there is no way to delete samples from the watch during a workout.
        guard let activeEnergySamples = samples as? [HKQuantitySample] else { return }
        sampleHandler(activeEnergySamples)
    }

    currentQuery = activeEnergyQuery
    healthStore.executeQuery(activeEnergyQuery)
}

func endWorkoutOnDate(endDate: NSDate) {
    workoutEndDate = endDate

    workoutButton.setTitle("Begin Workout")
    activeEnergyBurnedLabel.setText("0.0")

    if let query = currentQuery {
        healthStore.stopQuery(query)
    }

    saveWorkout()
}
requestAuthorizationToShareTypes函数

override func willActivate() {
    // This method is called when watch view controller is about to be visible to user.
    super.willActivate()

    // Only proceed if health data is available.
    guard HKHealthStore.isHealthDataAvailable() else { return }

    // We need to be able to write workouts, so they display as a standalone workout in the Activity app on iPhone.
    // We also need to be able to write Active Energy Burned to write samples to HealthKit to later associating with our app.
    let typesToShare = Set([
        HKObjectType.workoutType(),
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!])

    let typesToRead = Set([
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!])

    healthStore.requestAuthorizationToShareTypes(typesToShare, readTypes: typesToRead) { success, error in
        if let error = error where !success {
            print("You didn't allow HealthKit to access these read/write data types. In your app, try to handle this error gracefully when a user decides not to provide access. The error was: \(error.localizedDescription). If you're using a simulator, try it on a device.")
        }
    }
}
AppDelegate.swift

import UIKit
import HealthKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
let healthStore: HKHealthStore = HKHealthStore()

func applicationShouldRequestHealthAuthorization(application: UIApplication) {
    healthStore.handleAuthorizationForExtensionWithCompletion { success, error in
        if let error = error where !success {
            print("You didn't allow HealthKit to access these read/write data types. In your app, try to handle this error gracefully when a user decides not to provide access. The error was: \(error.localizedDescription). If you're using a simulator, try it on a device.")
        }
    }
}
}

您的应用程序需要请求授权才能读取和写入有功能量样本。在用户选择是否授权您的应用程序之前,授权将“未确定”。有关使用
requestAuthorizationToShareTypes:readTypes:completion:

请求授权的更多信息,请参阅,您是否已将iOS应用程序设置为通过watch应用程序处理healthkit授权?当您从Apple Watch请求使用healthkit类型的权限时,iOS应用程序上会显示一个权限对话框。但是,你需要告诉你的iOS应用程序,你希望你的apple watch会请求它。您可以使用AppDelegate文件中的以下代码执行此操作:

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

请注意,数据可以直接从手表的传感器(如心率和燃烧的卡路里)发送到healthkit,无需获得应用程序的许可。听起来您的权限错误是因为您试图读取数据(您还没有权限读取数据)。

好,这很简单。那么这意味着苹果没有在演示中编写它?因为我所有的数据都很好。如果我需要请求auth,为什么我一直在获取数据?你看过这个演示吗?看来苹果已经在InterfaceController.swift中请求授权共享类型了。我编辑了我的问题并将其粘贴在底部。所以我仍然不确定为什么我会在控制台中看到这个错误,即使所有数据似乎都正确地同步到活动应用程序和健康应用程序。是的。事实上,苹果已经建立了这一机制。正在发送和记录数据。我更新了我的问题,加入了AppDelegate,这样你就可以看到苹果的代码了。谢谢