4

我正在嘗試構建一個顯示用戶健康數據的watchOS 2複雜功能,例如步驟(但理論上它應該能夠顯示用戶賦予應用程序許可查看的任何健康數據)。當複雜功能首次啓動時,我可以查詢Healthkit並獲取我想要的所有數據,因爲首次啓動被認爲處於前臺。但是,當新的健康數據可用時,我無法在後臺檢索HealthKit數據。有兩個地方我可以得到這些數據,手錶和iPhone。如何顯示在後臺刷新的併發症的HealthKit數據?

當從getNextRequestedUpdateDateWithHandler中設置的日期觸發併發症的後臺刷新時,我試圖從手錶中獲取數據。但是,當我調用HKHealthStore的execute方法時,如果應用程序(或在這種情況下併發症)正在運行後臺,它不會返回任何查詢結果。我還嘗試設置一個HKAnchoredObject查詢,當過程恢復時應立即返回結果,但這似乎也不會返回任何結果,除非手動在手錶上啓動應用程序擴展。這裏是我的手錶配對碼,從我ExtensionDelegate的init方法稱爲請求醫療包的權限後:

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, samples, deletedObjects, anchor, error) -> Void in 
     // Handle update notifications after the query has initially run 
     self.handleStepResults(query, samples: samples, deletedObjects: deletedObjects, anchor: anchor, 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方法也會拒絕返回任何查詢結果。我認爲這是有道理的,可能沒有辦法解決這個問題,因爲Apple's docs提到當設備被鎖定並且你無法從商店讀取(只寫)時Health Store被加密。但在手錶的情況下,當它在某些手腕上時,它並未鎖定,屏幕剛關閉。

有沒有人知道如何獲得HealthKit更新,當在後臺發生刷新時出現併發症,無論是在iOS還是在watchOS 2上?

+0

當您在手錶上進行併發症更新時,您試圖使用什麼比例? – Cobra

+0

它似乎並不重要。我嘗試了30分鐘和1小時,兩者都很好,但沒有使用HealthKit查詢。 – lehn0058

回答

7

經過廣泛的測試後,我確定目前這是不可能的。在watchOS 2上,當擴展或併發症在後臺運行時,Apple似乎完全禁用HealthKit查詢以返回結果。這包括從遠程通知執行,Watch連接,以及複雜計劃刷新。如果屏幕關閉且設備設置了密碼,則iPhone的HealthKit查詢失敗。查詢失敗,因爲健康數據存儲在設備被鎖定時被加密。即使啓用了觀察者查詢和後臺交付,查詢也會失敗。您可能會收到有關更改的通知,但在iPhone解鎖之前無法查詢更改(因爲數據已加密)。

其他應用程序通過直接查詢計步器(CMPedometer)來顯示與健康相關的數據,例如步數和步行距離,這些數據可以在這些背景模式下訪問。

可以做一個複雜功能,在後臺更新排他性的iPhone用戶誰沒有在他們的設備上設置的密碼,但這似乎是一個可怕的想法,促進。

相關問題