cao xiangyu cao xiangyu - 18 days ago 24
iOS Question

How to reset pedometer to 0 in Swift? I am using CMPedometer()

I want to add a button in my app and reset the pedometer to 0 by pressing it. Now I just finish a common pedometer, my code is below:

@IBOutlet weak var step: UILabel!

var days:[String] = []
var stepsTaken:[Int] = []
let activityManager = CMMotionActivityManager()
var pedoMeter = CMPedometer()

override func viewDidLoad() {
super.viewDidLoad()

var cal = Calendar.current
var comps = cal.dateComponents([.year, .month, .day, .hour, .minute, .second], from: Date())
comps.hour = 0
comps.minute = 0
comps.second = 0
let timeZone = NSTimeZone.system
cal.timeZone = timeZone
let midnight = cal.date(from: comps)!

if(CMPedometer.isStepCountingAvailable()){


self.pedoMeter.startUpdates(from: midnight) { (data: CMPedometerData?, error) -> Void in
DispatchQueue.main.async(execute: { () -> Void in
if(error == nil){
self.step.text = "\(data!.numberOfSteps)"
}
})
}
} }

Answer

You cannot "reset the pedometer". It is a piece of hardware.

What you do is write software. No law says that you have to maintain or display as your count the same number that the pedometer is keeping as its count. Maintain your own separate count as an instance property.