HKOBServerQuery未确定授权失败



试图为HealthKit设置观察者查询时,对于许多用户,我会遇到Authorization not Determined的错误。

研究了一段时间后,我看到这样的错误只有在尝试将(共享)数据写入HealthKit时才会发生。根据Apple HK文档 - 尝试阅读用户未授予权限的数据时,我根本不应该获取任何数据(没有任何错误),就好像没有新数据一样。

这是我用来设置观察者查询的代码:

class func setObserver(healthKitManager manager: HealthKitManager, forType type: HKSampleType?, withPredicateAfterFetching predicate: NSPredicate?) {
    guard type != nil && manager.healthStore != nil else {
        return
    }
    let query = HKObserverQuery.init(sampleType: type!, predicate: nil) { (query, completionHandler, error) in
        guard error == nil else {
            SimpleEvent(name: "HKObserverQuery failed", param: "type:(type!.identifier), error: (error!.localizedDescription)").fire()
            return
        }
        let anchoredQuery = HKAnchoredObjectQuery.init(type: type!, predicate: predicate, anchor: manager.getHealthDataAnchor(forType: type!), limit: HKObjectQueryNoLimit, resultsHandler: { (query, results, deletedObjects, newAnchor, error) in
            guard error == nil else {
                SimpleEvent(name: "HKAnchoredQuery failed", param: "type:(type!.identifier), error: (error!.localizedDescription)").fire()
                return
            }  
   \ Code that saves the HK Data
            completionHandler()
        })
        manager.healthStore!.execute(anchoredQuery)
    }
    manager.healthStore!.execute(query)
    manager.observerQueries.append(query)
}

错误在HKObserverQuery failed事件上捕获。

正如我提到的,权限请求不应影响数据读数,但无论如何是该代码:

func requestAuthorizationToShareTypesAndReadTypes(withSuccess successBlock: @escaping () -> Void, failure failureBlock: @escaping () -> Void) {
    self.healthStore?.requestAuthorization(toShare: writeDataTypes as! Set<HKSampleType>, read: readDataTypes as! Set<HKObjectType>, completion: { (success, error) in
        if !success || error != nil {
            SimpleEvent.init(name: "HK-requestAuthorization", param: error!.localizedDescription)
            NSLog("You didn't allow HealthKit to access these read/write data types. The error was: %@.", error as! NSError);
            failureBlock()
            return;
        }
        self.userHaveBeenPromptWithPermissionsScreenAtLeastOnce = true
        self.enableBackgroundDelivery()
        successBlock()
    })
}

这是我启用背景交付的部分

func enableBackgroundDelivery() {
    for type in self.readDataTypes! {
        if let type = type as? HKSampleType {
            self.enableBackgroundDelivery(forSampleType: type)
        }
    }
}
func enableBackgroundDelivery(forSampleType type:HKSampleType) {
    self.healthStore?.enableBackgroundDelivery(for: type, frequency: HKUpdateFrequency.immediate, withCompletion: { (success, error) in
        if error != nil {
            print(String.init(format: "HK-BackgoundError: error:%@, user: %@, type: %@", error!.localizedDescription, NRManager.shared().username, type.identifier))
        }
    })
}

需要授权才能从HealthKit读取数据。如果未确定指示授权的错误,则指示没有提示用户授权您的应用程序读取或编写您要查询的类型的示例。

我通过添加:

解决了这一点
HKObjectType.activitySummaryType()

我的 hkhealthstore的读取类型 requestAuthorization()

相关内容

  • 没有找到相关文章

最新更新