IOS实现基于CMPedometer的计步器-创新互联

CMStepCount类在IOS8已经不推荐使用了,IOS8推荐使用CMPedometer类来处理用户健康和运动信息.下面是一个小小的demo来演示下,如何使用它,以及一些注意事项.

成都创新互联公司主营灌南网站建设的网络公司,主营网站建设方案,APP应用开发,灌南h5微信小程序开发搭建,灌南网站营销推广欢迎灌南等地区企业咨询
#import "ViewController.h" 
#import  
 
@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

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站标题:IOS实现基于CMPedometer的计步器-创新互联
网页网址:http://pwwzsj.com/article/cdosee.html