0

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.

Community
  • 1
  • 1
Byte
  • 629
  • 2
  • 11
  • 29
  • Possible duplicate of [Ignore manual entries from Apple Health app as Data Source](http://stackoverflow.com/questions/31184628/ignore-manual-entries-from-apple-health-app-as-data-source) – Allan Aug 11 '16 at 15:57
  • That solution is outdated not working now. – Byte Aug 12 '16 at 04:35
  • Could you elaborate on why it's outdated? – Allan Aug 12 '16 at 18:47

1 Answers1

0

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)
}
Byte
  • 629
  • 2
  • 11
  • 29