CMPedometer 实现计步
CMPedometer:統計某段時間內用戶步數,距離信息,甚至計算用戶爬了多少級樓梯 在iOS8.0及以后系統可以使用(8.0以前用CMSetpCounter)
要使用CMPedometeri 需要我們在對應類中導入CoreMotion 并聲明屬性
#import <CoreMotion/CoreMotion.h>
@property (nonatomic, strong) CMPedometer * pedonmeter;
?
在ViewDidLoad中初始化
self.pedonmeter = [[CMPedometer alloc]init];
?
?
判斷計步方法是否可用
if (!([CMPedometer?isStepCountingAvailable] || [CMMotionActivityManager isActivityAvailable])) {
?? ? ? ?
? ? ? ? NSString *msg = @"抱歉,不能運行哦,只支持iOS 8.0以上及iPhone5s以上機型.";
? ? ? ? UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No!"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? message:msg
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? delegate:nil
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cancelButtonTitle:@"OK"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? otherButtonTitles:nil];
? ? ? ? [alert show];
?? ? ? ?
? ? }else{
?? ? ? ?
? ? ? ?// do counter
? ? }
?
?
// 獲取前7天的數據
- (void)getStepOneWeek
{
? ? __weak ViewController * weakSelf = self; ?// 弱引用 防止內存泄漏
? ??
? ? if ([CMPedometer isStepCountingAvailable]) { // 判斷能否計步
? ? ? ? NSMutableString * dateStr = [NSMutableString string]; //可變數組記錄每天步數
? ? ? ??
? ? ? ? for (int i = 6; i >= 0; i --) { ? // for循環 取出每天的步數
?? ? ? ? ? ?
? ? ? ? NSCalendar *calendar = [NSCalendar currentCalendar];
? ? ? ? ? ? NSDate *now = [NSDate date];
? ? ? ? NSLog(@"234567890----%@", now);
? ? ? ??NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
?? ? ? ? ? ?
? ? ? ? ? ? NSDate *nowDate = [calendar dateFromComponents:components];
? ? ? ? ? ? NSDate * startTempDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:-i toDate:nowDate options:0];
? ? ? ? ? ? // 結束日期
? ? ? ? ? ? NSDate *endTempDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startTempDate options:0];
? ? ? ? ? ? NSDate * startDate = [self getStartTimeWithDate:startTempDate]; ?// 將日期轉為某年某月某天00:00:00
? ? ? ? ? ? NSDate * endDate = [self getStartTimeWithDate:endTempDate];
? ? ? ? ? ? NSLog(@"%@? %@ ", startDate, endDate);
// 從開始時間到結束時間的總步數
? ? ? ? ? ? [self.pedometer queryPedometerDataFromDate:startDate toDate:endDate withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
? ? ? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? ? ? ? ? if (error) {
? ? ? ? ? ? ? ? ? ? ? ? NSLog(@"%@", error); // 出錯 ?錯誤信息 ?如果是Error Domain=CMErrorDomain Code=103 "The operation couldn’t be completed. (CMErrorDomain error 103.) ?去看pedometer是不是成員變量 并在viewDidiLoad:中創建實例
? ? ? ? ? ? ? ? ? ? ? ? UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"No!" message:@"error" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
? ? ? ? ? ? ? ? ? ? ? ? [error show];
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else {
?? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? [dateStr appendFormat:@"%@+%.2f " ,pedometerData.numberOfSteps,[pedometerData.distance doubleValue]];
? ? ? ? ? ? ? ? ? ? ? ? NSLog(@"%@", dateStr);
? ? ? ? ? ? ? ? ? ? ? ? if (i == 0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? weakSelf.totalLabel.text = [NSString stringWithFormat:@"%@",dateStr];
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? }];
?? ? ? ? ? ?
? ? ? ? }
? ? }
}
?
步數收集到了!
然后就是實時步數記錄
?
?
// 今天的實時數據
- (void)getTodayData
{
? ? __weak ViewController * weakSelf = self;
?? ?
? ? if ([CMPedometer isStepCountingAvailable]) {
?
? ? ? ? NSDate * date = [self getStartTime]; ?// 獲取今天的00:00:00
? ? ? ? NSLog(@"formDate:%@",date);
?
// 從data開始的實時步數記錄
? ? ? ? [self.pedometer startPedometerUpdatesFromDate:date withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
? ? ? ? ? ? NSLog(@"距離%@+步數%@",pedometerData.distance, pedometerData.numberOfSteps);
? ? ? ? ? ? if (error) {
? ? ? ? ? ? ? ? NSLog(@"%@",error);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
//?distance 走的距離 ??numberOfSteps ?步數
? ? ? ? ? ? ? ? ? ? NSString * str = [NSString stringWithFormat:@"%.2f+%@",[pedometerData.distance doubleValue], pedometerData.numberOfSteps];
? ? ? ? ? ? ? ? ? ? weakSelf.distanceLabel.text = str;
? ? ? ? ? ? ? ? });
? ? ? ? ? ? }
? ? ? ? }];
?? ? ? ?
? ? }
}
?
OK 實時記錄和往日查詢都Ok了!! ?
?
Error Domain=CMErrorDomain Code=105 ?該錯誤是因為沒有設置infoplist文件中的Motion隱私選項 或者是未在設置->隱私->運動與健康 中打開權限。
另一個錯誤code碼是104(或者103?) ?是因為?pedometer 不是property屬性(全局屬性) 。
?
?上面因為取時間轉化問題 可能會有步數差距。 需要根據自己的需要將時間調整好。
?
?
?
轉載于:https://www.cnblogs.com/LoveStoryJX/p/6605849.html
總結
以上是生活随笔為你收集整理的CMPedometer 实现计步的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rtsp的移植
- 下一篇: python爬虫爬取百度贴吧图片,req