CMPedometer(计步器)使用,获取用户行走步数、距离等信息

用途

CMPedometeri,iOS8.0及以后系统可以使用,统计某段时间内用户步数,距离信息。

代码实现

引入框架CoreMotion.framework

#import <CoreMotion/CoreMotion.h>

@property (strong, nonatomic) CMPedometer *pedonmeter;

_pedonmeter = [[CMPedometer alloc] init];
    if ([CMPedometer isStepCountingAvailable]) {
        [_pedonmeter queryPedometerDataFromDate:[NSDate dateWithTimeIntervalSinceNow:-60*60*24*2] toDate:[NSDate dateWithTimeIntervalSinceNow:-60*60*24*1] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
            if (error)
            {
                NSLog(@"error===%@",error);
            }
            else {
                NSLog(@"步数===%@",pedometerData.numberOfSteps);
                NSLog(@"距离===%@",pedometerData.distance);
            }
        }];
    }else{
        NSLog(@"不可用===");
    }

错误处理

如果出现以下错误,是因为CMPedometer回调时,对象已经没有了,所以声明为属性就可以了。

Error Domain=CMErrorDomain Code=103 "The operation couldn’t be completed. (CMErrorDomain error 103.)"


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值