source - xcode swift step tracker app tutorial



CMPedometer in iOS 10 (1)

I'm trying to access CMPedometer data in iOS 10 and I've followed a few tutorials and I just can't seem to get anything to work. I've never tried to access this information before so don't really know where to start. I'd love some help on getting something setup. Any help you can provide will be greatly appreciated.

I want to do both live updates and also access information form the passed but only within the same day.

This currently doesn't 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()
    // Do any additional setup after loading the view, typically from a nib.
    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)")
                    //self.step.text = "\(data!.numberOfSteps)"
                }
              })
           }
       }

    }
}

I've been playing around with some things and realised that if the simulator is open as well a big error message appears. However if its closed and running only on my phone the error is:

2016-07-16 18:13:38.054710 test[419:37870] [access] private


You are using a beta version of Xcode, the log is common for all users. Nothing to worry about it. Edit your code lil bit.

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 arch(i386) || arch(x86_64) && os(iOS)

            // Simulator

        #else

            // Run only in Physical Device, iOS

            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)")
                            //self.step.text = "\(data!.numberOfSteps)"
                        }
                    })
                }
            }
        #endif
    }
}

Add key Privacy - Motion Usage Description in info.plist, with String value YES.

Try running the app again in iPhone and check.