IOS pedometer and ios Pedometer
This blog introduces the popular "pedometer"-simple knowledge points.
The implementation of the pedometer is changed at the beginning of ios8.
However, I will give a brief introduction to the previous sections.
IOS 8-
/// ViewController. m // CX pedometer /// Created by ma c on 16/4/12. // Copyright©2016 bjsxt. all rights reserved. // # import "ViewController. h "# import <CoreMotion/CoreMotion. h> @ interface ViewController () @ property (nonatomic, strong) CMStepCounter * counter; @ end @ implementation ViewController # pragma mark-<lazy loading>-(CMStepCounter *) counter {if (! _ Counter) {_ counter = [[CMStepCounter alloc] init];} return _ counter;}-(void) viewDidLoad {[super viewDidLoad]; // determine whether the pedometer is available if (! [CMStepCounter isStepCountingAvailable]) {NSLog (@ "pedometer unavailable"); return ;} // start the counting process // startStepCountingUpdatesToQueue specifies the thread in which the execution is performed // updateOn corresponds to the block [self. counter Timeout: [NSOperationQueue mainQueue] updateOn: 2 withHandler: ^ (NSInteger numberOfSteps, NSDate * _ Nonnull timestamp, NSError * _ Nullable error) {if (error) {return ;} NSLog (@ "some operations can be performed here") ;}] ;}@ end
IOS 8 +
/// ViewController. m // CX pedometer /// Created by ma c on 16/4/12. // Copyright©2016 bjsxt. all rights reserved. // # import "ViewController. h "# import <CoreMotion/CoreMotion. h> @ interface ViewController () @ property (nonatomic, strong) CMPedometer * meter; @ end @ implementation ViewController # pragma mark-<lazy loading>-(CMPedometer *) meter {if (! _ Meter) {_ meter = [[CMPedometer alloc] init];} return _ meter;}-(void) viewDidLoad {[super viewDidLoad]; // determine whether the pedometer is available if (! [CMPedometer isStepCountingAvailable]) {NSLog (@ "pedometer unavailable"); return ;}// start step counting // The number of parts and distance within a period of time [self. meter alert: [NSDate date] toDate: [NSDate dateWithTimeIntervalSinceNow: 60*60] withHandler: ^ (CMPedometerData * _ Nullable pedometerData, NSError * _ Nullable error) {if) {NSLog (@ "error = % @", error);} else {NSLog (@ "number of steps = % @", pedometerData. numberOfSteps); NSLog (@ "distance ==%@", pedometerData. distance) ;}}] ;}@ end