Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios HKObserverQuery失败,未确定授权_Ios_Swift_Authorization_Healthkit_Hkobserverquery - Fatal编程技术网

Ios HKObserverQuery失败,未确定授权

Ios HKObserverQuery失败,未确定授权,ios,swift,authorization,healthkit,hkobserverquery,Ios,Swift,Authorization,Healthkit,Hkobserverquery,当尝试为Healthkit设置一个observer查询时,对于许多用户,我得到了一个未确定授权的错误 经过一段时间的研究,我发现只有在尝试向Healthkit写入(共享)数据时才会发生这样的错误。根据Apple HK文档,当尝试读取用户未授予权限的数据时,我不应该获取任何数据(没有任何错误),就好像没有新数据一样 下面是我用来设置观察者查询的代码: class func setObserver(healthKitManager manager: HealthKitManager, forType

当尝试为Healthkit设置一个observer查询时,对于许多用户,我得到了一个
未确定授权的错误

经过一段时间的研究,我发现只有在尝试向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()
    })
}

从HealthKit读取数据需要授权。如果您收到一个错误,表明未确定授权,则表明未提示用户授权您的应用程序读取或写入您要查询的类型的样本。

我通过添加以下内容解决了此问题:

HKObjectType.activitySummaryType()

对于myHKHealthStorerequestAuthorization()

的阅读类型,谢谢,因为某种原因,在版本更新之后,已经批准了hk权限的用户必须重新批准它们。
HKObjectType.activitySummaryType()