iosswiftswift2healthkithkhealthstore

Getting only AutoDetected activities from healthkit


I am using health-kit in my application to read user's steps and activities. Every this in okay But I want to read only auto detected activities and steps. Currently I get all the data weather it was manually entereed or auto detected by health application. This is my code so far

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)
}

But where and how can I check the data was user entered or auto detected? I have seen This Question as well but its in objective-c and I was not able to fully understand it so please Guide me on this.


Solution

  • I solved the problem on my own. This is how I done it.

    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)
    }