chicmaggot
chicmaggot

Reputation: 51

CMPedometer is not running

My CMPedometer is not running.

The code before it and after it gets run, but it itself does not work. I get no warning or exception. I'm testing it on a real 5s.

I've tried both querydata and startpedometerupdates.

I am importing core motion and the library is linked.

Any help?

if ([CMPedometer isStepCountingAvailable] == YES)
    {
    CMPedometer *cmped;
    [cmped queryPedometerDataFromDate:start toDate:[NSDate date] withHandler:^(CMPedometerData *pedometerData, NSError *error){
        stepslabel.text = [pedometerData.numberOfSteps stringValue];
    }];

    }

Upvotes: 2

Views: 484

Answers (2)

progrmr
progrmr

Reputation: 77191

The problem with the original code above is the cmped variable gets deallocated at the end of the if statement, so the query is destroyed before it finishes. By changing it to a strong property, it is retained in memory for the life of the class.

Upvotes: 2

chicmaggot
chicmaggot

Reputation: 51

It seems really odd but I got it working by not declaring in the .h or before using it. What worked was declaring as @property CMPedometer *cmped; right after interface

Upvotes: 1

Related Questions