Danial Kosarifa
Danial Kosarifa

Reputation: 1074

CMPedometer Steps not Available . why?

I am trying to use the following code to get the number of steps using the coremotion library . But as for my output I only get the number of step are not available . I am not sure where am I making mistake and this is my first time using this library . I suspect something must be wrong with my date but I am not sure

 @IBAction func btn(_ sender: Any) {

    let calendar = Calendar.current
    let twoDaysAgo = calendar.date(byAdding: .day, value: -2, to: Date())
    pedometer.queryPedometerData(from: twoDaysAgo!, to: Date()) { (data, error) in
        if(error != nil){
            print(error)
        }else{
            if (data != nil ){
                print(data?.numberOfSteps)

            }else{
                print("nil")
            }
        }
    }
}

I've seen other similar questions to mine and they didnt help .my testing device fully support the sensor but the problem is that I receive null for the number of steps . Any idae how can I fix that ?

Upvotes: 0

Views: 1209

Answers (1)

Damien
Damien

Reputation: 3362

You should check if the pedometer is available on the device with isStepCountingAvailable() CMPedometer function

Also, the function startUpdates(from:withHandler:) retrieve datas from the date you give to the present moment. In your case, you set that date to right now, so there is no new datas.

If you want to retrieve datas for a specific start and end date, use you can use queryPedometerData(from:to:withHandler:)

Here's some documentation on CMPedometer :https://developer.apple.com/documentation/coremotion/cmpedometer

Upvotes: 2

Related Questions