簡體   English   中英

從Healthkit僅獲取自動檢測的活動

[英]Getting only AutoDetected activities from healthkit

我在應用程序中使用Health-kit來讀取用戶的步驟和活動。 一切正常,但是我只想閱讀自動檢測到的活動和步驟。 目前,我得到了健康應用程序手動輸入或自動檢測到的所有數據天氣。 到目前為止,這是我的代碼

func todaySteps(completion: (Double, NSError?) -> () )
{   
    let type = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount) // The type of data we are requesting

    let date = NSDate()
    print(date)
    let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    let newDate = cal.startOfDayForDate(date)
    print(newDate)
    let predicate = HKQuery.predicateForSamplesWithStartDate(newDate, endDate: NSDate(), options: .None) // Our search predicate which will fetch all steps taken today

    let query = HKSampleQuery(sampleType: type!, predicate: predicate, limit: 0, sortDescriptors: nil) { query, results, error in
        var steps: Double = 0

        if results?.count > 0
        {
            for result in results as! [HKQuantitySample]
            {
                steps += result.quantity.doubleValueForUnit(HKUnit.countUnit())
            }
        }

        completion(steps, error)
    }

    executeQuery(query)
}

但是,在哪里以及如何檢查用戶輸入或自動檢測到的數據? 我也已經看過這個問題,但是在Objective-C中卻沒有,我無法完全理解它,因此請引導我。

我自己解決了這個問題。 這就是我的方法。

func todaySteps(completion: (Double, NSError?) -> () )
{ 
    let type = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount) 

    let date = NSDate()
    print(date)
    let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    let newDate = cal.startOfDayForDate(date)
    print(newDate)
    let predicate = HKQuery.predicateForSamplesWithStartDate(newDate, endDate: NSDate(), options: .None)

    let query = HKSampleQuery(sampleType: type!, predicate: predicate, limit: 0, sortDescriptors: nil) { query, results, error in
        var steps: Double = 0
        if results?.count > 0
        {
            for result in results as! [HKQuantitySample]
            {
                print("Steps \(result.quantity.doubleValueForUnit(HKUnit.countUnit()))")

                // checking and truncating manually added steps
                if result.metadata != nil {
                    // Theses steps were entered manually
                }
                else{
                    // adding steps to get total steps of the day
                    steps += result.quantity.doubleValueForUnit(HKUnit.countUnit())
                }

            }
        }
        completion(steps, error)
    }      
    executeQuery(query)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM