There seems to be a bug in the CMPedometer queryPedometerData() method. The method is returning 0 steps for certain end times, but the same query with the timestamp 1 second higher or lower returns the correct number of steps
e.g.
self.getStepsBetweenDates(NSDate(timeIntervalSince1970: 1543392126) as Date, date2: NSDate(timeIntervalSince1970: 1543393044) as Date) returns (Int) 1488
self.getStepsBetweenDates(NSDate(timeIntervalSince1970: 1543392126) as Date, date2: NSDate(timeIntervalSince1970: 1543393045) as Date) returns (Int) 0
self.getStepsBetweenDates(NSDate(timeIntervalSince1970: 1543392126) as Date, date2: NSDate(timeIntervalSince1970: 1543393046) as Date) returns (Int) 1488
getStepsBetweenDates method looks like this
func getStepsBetweenDates(_ date1: Date, date2: Date) -> Int{
let group = DispatchGroup()
group.enter()
var steps = 0
self.pedometer.queryPedometerData(from: date1, to: date2, withHandler: {
pData, error in
if let e = error{
print("Error querying pedometer", e.localizedDescription)
}else{
if let data = pData{
steps = Int(data.numberOfSteps)
}
group.leave()
}
})
_ = group.wait(timeout: DispatchTime.distantFuture)
return steps
}