1

I wish to restrict results returned from a HealthKit HKSampleQuery to those that have been input through my own app. Is there a way to specify results only with my application bundle identifier, and thereby exclude any other data sources returned from other applications?

Is there a way to specify this with an NSSortDescriptor or NSPredicate, as I have tried below?

func querySteps() {
    // let sort = NSSortDescriptor(key: "bundleIdentifier", ascending: true, selector: "com.companyName.appName:")
    // let resultPredicate = NSPredicate(format: "bundleIdentifier", "com.companyName.appName")

    let sampleQuery = HKSampleQuery(sampleType: healthKitManager.stepsCount!,
        predicate: nil,
        limit: 100,
        sortDescriptors: nil)
        { [unowned self] (query, results, error) in
            if let results = results as? [HKQuantitySample] {
                self.steps = results
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    self.tableView.reloadData()
                });

            }
    }

    healthStore?.executeQuery(sampleQuery)
}
Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
user46938
  • 59
  • 4

1 Answers1

3

Simple one line solution to the question above. Use HKQuery to create a predicate object that specifies the data's source:

let thePredicate = HKQuery.predicateForObjectsFromSource(HKSource.defaultSource())

And then swap out the nil predicate parameter value with thePredicate, in this case. Then, the results in your table view will show only your own app's HKQuery results.

Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
user46938
  • 59
  • 4