IOS pedometer and ios Pedometer

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.