-1

I am working on a health care application with a pedometer function. I am using the CMPedometer class to start counting step, but I am looking for a way that can set the pedometer's Step Count to 0 after midnight and start counting steps for the next day (when movement is detected) and so on.

I have tried messing around with NSDate classes but having a hard time actually connecting them to work out as explained above.

    myPedometer = CMPedometer()

    myPedometer.startUpdates(from: NSDate() as Date, withHandler: { (pedometerData, error) in
        if let e = error {
            print(e.localizedDescription)
            return
        }
        guard let data = pedometerData else {
            return
        }
        let myStep = data.numberOfSteps
        self.stepLabel.text = "\(myStep)"


        var date = NSDate

        if date = 24:00  {
            self.myPedometer.stopUpdates()
        }

I am not aware of how each day's step counts could possibly be reset even though I have tried using an if statement to stop pedometer updates.

Also, X-Code keeps sending error messages for the last if statement saying "Cannot assign value of type 'int' to type 'NSDate.Type'.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • 1
    I'm not able to answer your full question, but your immediate issues are that you are missing `()`: `var date = NSDate()` and you are using `=` in `if` when you should be using `==`'. Also, `24:00` isn't something you can compare a Date too. In Swift you should use `Date`, not `NSDate`. – vacawama Jul 14 '19 at 12:30

1 Answers1

0

Used this function to getting number of steps between 2 dates.

func queryPedometerData(from start: Date, 
                     to end: Date, 
            withHandler handler: @escaping CMPedometerHandler)

refer tutorial for more details : https://medium.com/simform-engineering/count-steps-with-cmpedometer-on-iwatch-94b61bc3b87e

SGDev
  • 2,256
  • 2
  • 13
  • 29