——————————————————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)
//
// }];
}