IOS 运作处理器数据的获取

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
——————————————————iOS 运作处理器数据的获取 —————步数获取------------------
UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 568/2)];
[label setBackgroundColor:[UIColor grayColor]]; label.textColor =[UIColor redColor];
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
if ([CMStepCounter isStepCountingAvailable])
{
NSLog(@"isStepCountingAvailable!!!");
NSOperationQueue *queueStep = [[NSOperationQueue alloc] init] ; //20步数,更新一次
[_cmStepCounter startStepCountingUpdatesToQueue:queueStep updateOn:20 withHandler:^(NSInteger numberOfSteps, NSDate *timestamp, NSError *error) { NSLog(@"numberOfSteps==%ld,timestamp==%@",(long)numberOfSteps,timestamp);
_steps = numberOfSteps;
label.text = [NSString stringWithFormat:@"%ld",_steps];
}];
}else{
NSLog(@"isNOT StepCountingAvailable");
}
- (IBAction)doSomeThingsAction:(id)sender {
__block NSInteger stepsNumber = 0;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *tempDate = [dateFormatter dateFromString:@"2014-08-31 20:20:20"];
NSOperationQueue *queueStep = [[NSOperationQueue alloc] init] ;
[_stepManager queryStepCountStartingFrom:tempDate to:[NSDate date] toQueue:queueStep withHandler:^(NSInteger numberOfSteps, NSError *error)
{
if (!error)
{
stepsNumber = numberOfSteps;
dispatch_async(dispatch_get_main_queue(), ^{
self.countLabel.text = [NSString stringWithFormat:@"%ld",stepsNumber];
});
}else{
NSLog(@"%@",error);
} }];
[_activityManager queryActivityStartingFromDate:tempDate toDate:[NSDate date] toQueue:queueStep withHandler:^(NSArray *activities, NSError *error) {
if (!error) {
dispatch_async(dispatch_get_main_queue(), ^{ CMMotionActivity *tempActivity = [activities objectAtIndex:0]; NSLog(@"%@",tempActivity);
});
}else{ NSLog(@"%@",error); } }]; }
————2
在ios8中,ios7新增加的计步器CMStepCounter已经过时,而用CMPedometer代替。
具体代码(CMStepCounter)
if ([CMStepCounterisStepCountingAvailable]) {
self.stepCounter=[[CMStepCounter alloc]init];
NSOperationQueue *queue=[[NSOperationQueue alloc]init];
[self.stepCounter startStepCountingUpdatesToQueue:queue updateOn:5 withHandler:^(NSInteger numberOfSteps, NSDate *_Nonnull timestamp, NSError * _Nullable error) {
self.stepLabel.text=[NSString stringWithFormat:@"用户已经行走了【%d】步!",numberOfSteps];
}];
}
(CMPedometer)
if ([CMPedometer isStepCountingAvailable]) {
self.pedometer=[[CMPedometer alloc]init];
[self.pedometer startPedometerUpdatesFromDate:[NSDate date] withHandler:^(CMPedometerData *_Nullable pedometerData, NSError * _Nullable error) {
CMPedometerData *data=(CMPedometerData *)pedometerData;
NSNumber *number=data.numberOfSteps;
self.stepLabel.text=[NSString stringWithFormat:@"用户已经行走了【%@】步!",number];
}];
}
————————————————可以实现的时间间隔 获取步数 --------
//ios8
if
([CMStepCounter
isStepCountingAvailable])
{
CMStepCounter* stepCounter=[[CMStepCounter alloc]init];
NSOperationQueue *queue=[[NSOperationQueue alloc]init];
NSDate * newdate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:newdate];
components.hour = 0;
components.minute = 0;
components.second = 0;
// components.nanosecond = 0 not available in iOS
NSTimeInterval ts = (double)(int)[[calendar dateFromComponents:components] timeIntervalSince1970];
NSDate * olddate = [NSDate dateWithTimeIntervalSince1970:ts];//[NSDate dateWithTimeInterval:-24*60*60
// 时间间隔获取步数
[stepCounter queryStepCountStartingFrom:olddate to:newdate toQueue:queue withHandler:^(NSInteger numberOfSteps, NSError * _Nullable error) {
NSLog(@"3行走了多少步%ld",(long)numberOfSteps)//成功, 时间转换下正常就可以
}];
// [stepCounter startStepCountingUpdatesToQueue:queue updateOn:5 withHandler:^(NSInteger numberOfSteps, NSDate * _Nonnull timestamp, NSError * _Nullable error) {
//
// NSLog(@"2行走了多少步%ld",(long)numberOfSteps)
//
// }];
}
//ios7
if ([CMPedometer isStepCountingAvailable]) {
CMPedometer* pedometer=[[CMPedometer alloc]init];
NSDate * newdate = [NSDate date];
NSDate * olddate = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:newdate];
// 时间间隔获取步数
[pedometer queryPedometerDataFromDate:olddate toDate:newdate withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
CMPedometerData *data=(CMPedometerData *)pedometerData;
NSNumber *number=data.numberOfSteps;
NSLog(@"4行走了多少步%ld",(long)number)
}];
// [pedometer startPedometerUpdatesFromDate:[NSDate date] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
//
// CMPedometerData *data=(CMPedometerData *)pedometerData;
//
// NSNumber *number=data.numberOfSteps;
//
// NSLog(@"2行走了多少步%ld",(long)number)
//
// }];
}
Author

陈昭

Posted on

2016-08-24

Updated on

2021-12-27

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Kommentare

You forgot to set the shortname for Disqus. Please set it in _config.yml.