CMPedometer in iOS 10

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()
    // 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 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

+4
1

- Xcode, . . lil.

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
    }
}

Privacy - Motion Usage Description info.plist, String YES.

iPhone .

+7

Source: https://habr.com/ru/post/1648100/


All Articles