cmstepcount类在ios8已经不推荐使用了,ios8推荐使用cmpedometer类来处理用户健康和运动信息.下面是一个小小的demo来演示下,如何使用它,以及一些注意事项.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
#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]) { 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文件中加入你要访问用户健康和运动信息的描述,如下图
运行结果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/ling_fengxue/article/details/53780985