I am trying to access data CMPedometerin iOS 10 and I have been following several tutorials and I just can't get anything to work. I have never tried to access this information, so I donβt know where to start. I would really like to help with the setup. Any help you can provide would be greatly appreciated.
I want to do both live updates and receive information about the information received, but only on the same day.
It currently does not open and prints a massive error.
import UIKit
import CoreMotion
class ViewController: UIViewController {
var days:[String] = []
var stepsTaken:[Int] = []
let activityManager = CMMotionActivityManager()
let pedoMeter = CMPedometer()
override func viewDidLoad() {
super.viewDidLoad()
let cal = Calendar.current
var comps = cal.components([.year, .month, .day, .hour, .minute, .second], from: Date())
comps.hour = 0
comps.minute = 0
comps.second = 0
let timeZone = TimeZone.system
cal.timeZone = timeZone
let midnightOfToday = cal.date(from: comps)!
if(CMPedometer.isStepCountingAvailable()){
self.pedoMeter.startUpdates(from: midnightOfToday) { (data: CMPedometerData?, error) -> Void in
DispatchQueue.main.async(execute: { () -> Void in
if(error == nil){
print("\(data!.numberOfSteps)")
}
})
}
}
}
}
I played with some things and realized that if the simulator is open, a big error message appears. However, if it is closed and only works on my phone, the error is:
2016-07-16 18: 13: 38.054710 test [419: 37870] [access] private