Search code examples
iosiphoneswiftcore-motion

CMStepCounter returning 0 for all days when testing on an iPhone 5s


I am trying to make an app that displays the past 7 days of steps detected by the M7 chip using core motion on an iPhone 5s. The problem I am running into however is that I am getting 0 steps for any day that I try to get data for.

For now I am just trying to put the step count in an array, mainly for testing reasons. I have the following in my viewDidLoad method.

...
var mySteps = [Int]()

 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    for day in 1..<30 {
        mySteps.append((stepReporter.getStepsFrom(getDate(daysFromToday: day))))

        println("\(mySteps[day])")
     }
 }

The getDate method used above just allows me to get keep getting one day back in time by doing this:

func getDate(daysFromToday numberOfDays : Int) -> NSDate{

    var now = NSDate()

    var calendar = NSCalendar.autoupdatingCurrentCalendar()
    var components = calendar.components(NSCalendarUnit.DayCalendarUnit
        | NSCalendarUnit.MonthCalendarUnit
        | NSCalendarUnit.YearCalendarUnit,
        fromDate: now)
    var beginningOfToday = calendar.dateFromComponents(components)

    //println("the date marked is \(beginningOfToday)")

    components.day =  -1 * numberOfDays
    components.month = 0
    components.year = 0

    var newDate : NSDate = calendar.dateByAddingComponents(components, toDate: beginningOfToday, options: nil)

    return newDate
}

stepReporter is an instance of a class that uses a CMStepCounter object to get step counts

func getStepsFrom(date : NSDate) -> Int{

    var dayAfter = getDateDayAfter(date)

    stepCounter.queryStepCountStartingFrom(date, to: dayAfter, toQueue: stepQueue, withHandler: {numberOfSteps, error in
        if error{
            println(" \(error.localizedDescription)")
        }
        else{
            //self.numberOfStepsThatDay = numberOfSteps
            println("the number of steps from \(date) to \(newDate) is = \(numberOfSteps)")
        }
        })

    return numberOfStepsThatDay
}

The console is printing the following

the number of steps from 2014-07-22 04:00:00 +0000 to 2014-07-23 04:00:00 +0000 is = 270
the number of steps from 2014-07-08 04:00:00 +0000 to 2014-07-09 04:00:00 +0000 is = 0
the number of steps from 2014-07-07 04:00:00 +0000 to 2014-07-08 04:00:00 +0000 is = 0
the number of steps from 2014-07-06 04:00:00 +0000 to 2014-07-07 04:00:00 +0000 is = 0
the number of steps from 2014-07-21 04:00:00 +0000 to 2014-07-22 04:00:00 +0000 is = 0
the number of steps from 2014-07-05 04:00:00 +0000 to 2014-07-06 04:00:00 +0000 is = 0

So I am getting the correct data for the most recent day but getting 0 for all other days. Am I missing something or am I doing something wrong? There is nothing else running in the stepQueue. I used CMPedometer as well and got the same result. Any advise would be greatly appreciated. Thank you!


Solution

  • I installed 3 different apps that use the M7 chip and can confirm that the step count is the same I am getting from my app. I was concerned given that a previous step counter app that I had previously installed was giving me what I think is more accurate data. It could be and issue with iOS 8 beta or maybe the historical data simply is not there after restoring the phone. Thank you