IOS實現(xiàn)基于CMPedometer的計步器
更新時間:2018年06月25日 16:18:29 作者:ling_fengxue
這篇文章主要為大家詳細(xì)介紹了IOS實現(xiàn)基于CMPedometer的計步器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
CMStepCount類在IOS8已經(jīng)不推薦使用了,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文件中加入你要訪問用戶健康和運動信息的描述,如下圖

運行結(jié)果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS開發(fā)之TableView實現(xiàn)完整的分割線詳解
在iOS開發(fā)中, tableView是我們最常用的UI控件之一。所以這篇文章主要給大家詳細(xì)介紹了關(guān)于iOS中的TableView分割線,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12
iOS中利用CoreAnimation實現(xiàn)一個時間的進(jìn)度條效果
在iOS中實現(xiàn)進(jìn)度條通常都是通過不停的設(shè)置progress來完成的,這樣的進(jìn)度條適用于網(wǎng)絡(luò)加載(上傳下載文件、圖片等)。下面通過本文給大家介紹iOS中利用CoreAnimation實現(xiàn)一個時間的進(jìn)度條,需要的的朋友參考下吧2017-09-09
iOS 對NSMutableArray進(jìn)行排序和過濾的實例
下面小編就為大家分享一篇iOS 對NSMutableArray進(jìn)行排序和過濾的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01

