大佬教程收集整理的这篇文章主要介绍了ios – 如何在后台刷新的复杂功能上显示HealthKit数据?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
当从getNextRequestedUpdateDateWithHandler中设置的日期触发复杂的后台刷新时,我试图从手表本身获取数据.但是,当我调用HKHealthStore的执行@L_673_14@时,如果应用程序(或在这种情况下,它)不会返回任何查询结果并发症)正在运行后台.我还尝试设置一个HKAnchoredObject查询,该查询应该在进程恢复时立即返回我的结果,但除非我在手表上手动启动应用程序扩展,否则这似乎也不会返回任何结果.这是我的监视代码,在请求运行状况工具包权限后从我的ExtensionDelegate的init@L_673_14@调用:
func setupComplicationDataCache() { let Now = NSDate() var startDate: NSDate? = nil var interval: NSTimeInterval = 0 self.calendar.rangeOfUnit(NSCalendarUnit.Day,startDate: &startDate,interval: &interval,forDate: Now) let stepSampleType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypEIDentifierStepCount)! // Match samples with a start date after the workout start let preDicate = HKQuery.preDicateForSamplesWithStartDate(startDate,endDate: nil,options: .NonE) let query = HKAnchoredObjectQuery(type: stepSampleType,preDicate: preDicate,anchor: nil,limit: 0) { (query,samples,deletedObjects,anchor,error) -> Void in // Handle when the query first returns results self.handleStepResults(query,samples: samples,deletedObjects: deletedObjects,anchor: anchor,error: error) } query.updateHandler = { (query,error) -> Void in // Handle update notifications after the query has initially run self.handleStepResults(query,error: error) } self.healthStore.executeQuery(query); } func handleStepResults(query: HKAnchoredObjectQuery,samples: [HKSample]?,deletedObjects: [HKdeletedObject]?,anchor: HKQueryAnchor?,error: NSError?) { if error != nil { self.timelineModel.currentEntry = TimelineEntrymodel(value: NSnumber(int: -1),endDate: NSDate()) } else if samples == nil || samples?.count == 0 { self.timelineModel.currentEntry = TimelineEntrymodel(value: NSnumber(int: 0),endDate: NSDate()) } else { let newStepSamples = samples as! [HKQuantitySample] var stepCount = self.timelineModel.currentEntry.value.doubleValue var currentDate = self.timelineModel.currentEntry.endDate // Add the current entry to the collection of past entries self.timelineModel.pastEntries.append(self.timelineModel.currentEntry) // Add all new entries to the collection of past entries for result in newStepSamples { stepCount += result.quantity.doubleValueForUnit(self.countUnit) currentDate = result.endDate self.timelineModel.pastEntries.append(TimelineEntrymodel(value: NSnumber(double: stepCount),endDate: currentDatE)) } // Retrieve the latest sample as the current item self.timelineModel.currentEntry = self.timelineModel.pastEntries.popLast() if self.timelineModel.currentEntry == nil { self.timelineModel.currentEntry = TimelineEntrymodel(value: NSnumber(int: -3),endDate: NSDate()) } } // Reload the complication let complicationServer = CLKComplicationServer.sharedInstance() for complication in complicationServer.activeComplications { complicationServer.reloadTimelineForComplication(complication) } }
我也尝试使用HKObserverQuery从iPhone获取数据.我有观察者查询,可以每小时唤醒一次iPhone(步骤数据的最大时间).但是,如果在观察者完成处理程序执行我的步骤查询时iPhone被锁定,则HKHealthStore的execute@L_673_14@也拒绝返回任何查询结果.我认为这在这里是有道理的,并且可能没有办法@L_450_33@这个问题,因为Apple’s docs提到当设备被锁定而你无法从商店读取时(仅写入),Health Store会被加密.但是在手表的情况下,当它在某人的手腕上没有锁定时,屏幕才会关闭.
有没有人知道如何在后台进行刷新时,无论是在iOS还是在watchOS 2上,都会让HealthKit更新出现在并发症上?
显示健康包相关数据的其他应用程序(例如步数和步行距离)通过直接查询计步器(CMPedometer)来实现,这些数据可在这些背景模式下访问.
以上是大佬教程为你收集整理的ios – 如何在后台刷新的复杂功能上显示HealthKit数据?全部内容,希望文章能够帮你解决ios – 如何在后台刷新的复杂功能上显示HealthKit数据?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。