CMPedometer获取步数,获取的步数用户不能进行修改
第一步:配置info.plist里边的获取运动权限
<key>NSMotionUsageDescription</key> <string>请允许使用运动健康</string>
第二步:引入头文件
#import <CoreMotion/CoreMotion.h> @property (nonatomic, strong) CMPedometer * pedometer;
第三步:获取步数
// 获取步数 -(void)getHealthStep { if ([CMPedometer isStepCountingAvailable]) { _pedometer = [[CMPedometer alloc] init]; NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"yyyy-MM-dd"]; NSString *yestr = [df stringFromDate:[NSDate date]]; [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //今天0点时间 NSDate *d = [df dateFromString:[NSString stringWithFormat:@"%@ 00:00:00", yestr]]; //查询0点到当前时间的数据 [_pedometer queryPedometerDataFromDate:d toDate:[NSDate dateWithTimeIntervalSinceNow:0] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) { if (error) { NSLog(@"error ===%@", error); } else { NSLog(@"步数====%@", pedometerData.numberOfSteps); NSLog(@"距离====%@", pedometerData.distance); } }]; } else { NSLog(@"记步功能不可用"); } }