您好,登錄后才能下訂單哦!
CMStepCount類在IOS8已經不推薦使用了,IOS8推薦使用CMPedometer類來處理用戶健康和運動信息.下面是一個小小的demo來演示下,如何使用它,以及一些注意事項.
#import "ViewController.h" #import <CoreMotion/CoreMotion.h> @interface ViewController () @property (weak, nonatomic) IBOutlet UILabel *stepLabel; @property(nonatomic,strong) CMPedometer *stepter; @property (weak, nonatomic) IBOutlet UILabel *totalLabel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; if(![CMPedometer isStepCountingAvailable]) { NSLog(@"計步器不可用"); return; } _stepter =[[CMPedometer alloc]init]; NSTimeInterval secondsPerDay =24*60*60; NSDate *date =[NSDate date]; NSDate *yesterDay =[date dateByAddingTimeInterval:-secondsPerDay]; [_stepter startPedometerUpdatesFromDate:yesterDay withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) { if(error) { NSLog(@"error ==%@",error); }else { NSNumber *steps =pedometerData.numberOfSteps; NSNumber *distance =pedometerData.distance; NSDictionary *dic =@{ @"steps":steps, @"distance":distance }; NSLog(@"過去一天你一共走了%@步,一共%@米",steps,distance); [self performSelectorOnMainThread:@selector(refreshUI:) withObject:dic waitUntilDone:NO]; } }]; } -(void)refreshUI:(NSDictionary *)dic { NSNumber *distance =dic[@"distance"]; float meters =[distance floatValue]; self.stepLabel.text =[NSString stringWithFormat:@"%@",dic[@"steps"]]; self.totalLabel.text =[NSString stringWithFormat:@"%.2f",meters]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
此處還有一點需要注意:就是請在info.plist文件中加入你要訪問用戶健康和運動信息的描述,如下圖
運行結果如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。