gpt4 book ai didi

ios - HealthKit:收到由 body 事件,背景和终止状态触发的通知(Obj-C,iOS 10.3)

转载 作者:行者123 更新时间:2023-12-01 19:54:44 25 4
gpt4 key购买 nike

假设我们使用步数作为触发器-如果用户执行了10步,我希望弹出这样的通知。

这个问题已经在这里回答:
Healthkit background delivery when app is not running

但是,我无法使其正常运行。如果我在前台使用该应用程序,则步数将得到更新。如果我将应用程序留在后台,则重新启动应用程序后,步数似乎只会更新。因此,后台查询似乎无法正常工作。

我试过启用背景模式,没有效果;我已经尝试将应用程序在后台保留了几个小时,以为发送数据可能要花一些时间-什么都没有。

其他人说要尝试使用StatisticsCollectionQuery,因为StatisticsQuery损坏了-这有什么道理吗? StatisticsCollectionQuery对于我正在做的事情并不是很理想。

这是我的查询代码:

[self.healthStore enableBackgroundDeliveryForType:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]
frequency:HKUpdateFrequencyImmediate
withCompletion:^(BOOL success, NSError * _Nullable error) {
if (error) {

// Perform Proper Error Handling Here...
NSLog(@"*** An error occured while setting up background updates. %@ ***",
error.localizedDescription);
}
if (success == YES) {
backgroundEnabled = YES;
}
}];


// set up running observer

HKSampleType *sampleType =
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

HKObserverQuery *query =
[[HKObserverQuery alloc]
initWithSampleType:sampleType
predicate:nil
updateHandler:^(HKObserverQuery *query,
HKObserverQueryCompletionHandler completionHandler,
NSError *error) {

if (error) {

// Perform Proper Error Handling Here...
NSLog(@"*** An error occured while setting up the stepCount observer. %@ ***",
error.localizedDescription);
// Error popup

return;
}

// Take whatever steps are necessary to update your app's data and UI
// This may involve executing other queries
[self updateDailyStepCount];

// If you have subscribed for background updates you must call the completion handler here.
completionHandler();

}];

[self.healthStore executeQuery:query];

最佳答案

您请求HKUpdateFrequencyImmediate的事实表明您可能没有仔细研究enableBackgroundDeliveryForType:frequency:withCompletion:的文档。文档上有一个大的“注”框,该框说明步数永远不会比每小时更频繁地提供。

如果是这样,我建议您仔细阅读该部分中的“讨论”,因为HK既棘手又不明显。例如,您是否使用application:didFinishLaunchingWithOptions:调用此查询?您是否尝试过重新安装应用程序(我会从头开始删除并重新安装)。如果您没有三次调用completionHandler(),iOS将停止通知您。我建议构建一个小型,简单的应用程序,该应用程序除了记录步数外什么也不做;用它来处理所有细节。

另请参见HealthKit (iOS) wont deliver data in background (objC)。尚不清楚您是否已经在讨论中实现了这些要点。

关于ios - HealthKit:收到由 body 事件,背景和终止状态触发的通知(Obj-C,iOS 10.3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43401315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com