3

我正在研究健康包。在下面的代码正常工作的 iOS15 之前,我每小时都会得到生命体征,但在这个最新版本中,我每小时没有得到任何生命体征事件。

我在 iOS15 中面临一个问题,在每小时事件中没有收到任何事件。谁能帮我吗?根据苹果文档https://developer.apple.com/documentation/healthkit/hkhealthstore/1614175-enablebackgrounddelivery我已经在 XCode13 中启用了权限,但我仍然面临这个问题。

这是我的代码。(iOS15之前的工作代码)

private func startObservingStepCountChanges() {
    let sampleType =  HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)
    let query: HKObserverQuery = HKObserverQuery(sampleType: sampleType!, predicate: nil, updateHandler: self.stepChangeHandler)
    healthKitStore.execute(query)
    healthKitStore.enableBackgroundDelivery(for: sampleType!, frequency: .hourly, withCompletion: {(succeeded: Bool, error: Error!) in
        if succeeded{
            print("Enabled background delivery of stepcount changes")
        } else {
            if let theError = error{
                print("Failed to enable background delivery of stepcount changes. ")
                print("Error = \(theError)")
            }
        }
    } as (Bool, Error?) -> Void)
}

private func stepChangeHandler(query: HKObserverQuery!, completionHandler: HKObserverQueryCompletionHandler!, error: Error!) {

    // Flag to check the background handler is working or not
    print("Backgound Mode activated")
    fireTestPush()           
    completionHandler()
 }
4

1 回答 1

0

检查是否为您的应用启用了后台应用刷新。如果是,并且您仍然遇到此问题,请向 Apple 提交反馈助手报告,其中包含示例代码和 sysdiagnose。

于 2021-09-26T19:21:05.157 回答