当应用程序处于后台时,Healthkit查询不起作用



我需要观察Healthkit中的心率数据,然后相应地向用户发送通知。代码运行良好,但每次我都必须将应用程序带到前台才能获得通知。我需要不断地工作。这些查询似乎在后台不起作用。

我在网上查了几乎所有的东西,但找不到满足我需求的解决方案。任何帮助都将不胜感激!

编辑:还实现了后台模式。我检查了背景提取。仍然不起作用。。。

我在下面添加了我的代码片段:

func requestHealthKitPermission(successCallback: @escaping () -> Void) {
let sampleType: Set<HKSampleType> = [HealthKitService.heartRateType]
let healthKitStore = HKHealthStore()
healthKitStore.requestAuthorization(toShare: sampleType, read: sampleType) { (success, error) in
if success {
successCallback()
self.startObservingNewHeartRates()
}
if let error = error {
print("Error requesting health kit authorization: (error)")
}
}
}
func startObservingNewHeartRates() {
// open observer query
let query = HKObserverQuery(sampleType: HealthKitService.heartRateType,
predicate: nil) { (query, completionHandler, error) in
DispatchQueue.main.async {
self.updateHeartRates(completionHandler: completionHandler)
}
}
healthKitStore.execute(query)
//  Enable background delivery
healthKitStore.enableBackgroundDelivery(for: HealthKitService.heartRateType, frequency: .immediate) { (success, error) in
if let error = error {
print("could not enable background delivery: (error)")
}
if success {
print("background delivery enabled")
}
}
}
func updateHeartRates(completionHandler: @escaping () -> Void) {
var anchor: HKQueryAnchor?
if let data = UserDefaults.standard.object(forKey: Constants.UserDefaults.HEART_RATE_ANCHOR_KEY) as? Data {
anchor = try? NSKeyedUnarchiver.unarchivedObject(ofClass: HKQueryAnchor.self, from: data)
}
let sampleType =  HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!
let anchoredQuery = HKAnchoredObjectQuery(type: sampleType, predicate: nil, anchor: anchor, limit: HKObjectQueryNoLimit) {  query, newSamples, deletedSamples, newAnchor, error in
self.handleNewHeartRates(new: newSamples!, deleted: deletedSamples!)
if let newAnchor = newAnchor {
let data = try? NSKeyedArchiver.archivedData(withRootObject: newAnchor, requiringSecureCoding: false)
UserDefaults.standard.set(data, forKey: Constants.UserDefaults.HEART_RATE_ANCHOR_KEY)
print("Wrote new Anchor")
}
completionHandler()
}
healthKitStore.execute(anchoredQuery)
}
func handleNewHeartRates(new: [HKSample], deleted: [HKDeletedObject]) {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .full
let mapped = new.map { (sample) -> String? in
guard let hrSample: HKQuantitySample = sample as? HKQuantitySample else { return nil }
let heartRate = hrSample.quantity.doubleValue(for: HealthKitService.heartRateUnit)
if heartRate >= 70 { // TODO: extract to Constants
DispatchQueue.main.async {
self.viewController?.setupHeartRateNotifications()
}
}
return "(heartRate)bpm @ (dateFormatter.string(from: hrSample.startDate)) "
}.compactMap { $0 }
print(" ❤❤❤❤❤ new heartRate")
mapped.forEach { (line) in
print(line)
}
if let latestRecording = new.last {
let formattedDate = dateFormatter.string(from: latestRecording.endDate)
UserDefaults.standard.set(formattedDate, forKey: Constants.UserDefaults.LATEST_HEART_RATE_RECORDING_DATE_KEY)
}
}

}

尝试添加后台模式功能,并添加复选框"后台处理"one_answers"后台获取"。希望它能起作用。

相关内容

  • 没有找到相关文章

最新更新