日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

CMPedometer 实现计步

發(fā)布時(shí)間:2024/1/18 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CMPedometer 实现计步 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

CMPedometer:統(tǒng)計(jì)某段時(shí)間內(nèi)用戶步數(shù),距離信息,甚至計(jì)算用戶爬了多少級(jí)樓梯 在iOS8.0及以后系統(tǒng)可以使用(8.0以前用CMSetpCounter)

要使用CMPedometeri 需要我們?cè)趯?duì)應(yīng)類中導(dǎo)入CoreMotion 并聲明屬性

#import <CoreMotion/CoreMotion.h>

@property (nonatomic, strong) CMPedometer * pedonmeter;

?

在ViewDidLoad中初始化

self.pedonmeter = [[CMPedometer alloc]init];

?

?

判斷計(jì)步方法是否可用

if (!([CMPedometer?isStepCountingAvailable] || [CMMotionActivityManager isActivityAvailable])) {

?? ? ? ?

? ? ? ? NSString *msg = @"抱歉,不能運(yùn)行哦,只支持iOS 8.0以上及iPhone5s以上機(jī)型.";

? ? ? ? UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No!"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? message:msg

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? delegate:nil

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cancelButtonTitle:@"OK"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? otherButtonTitles:nil];

? ? ? ? [alert show];

?? ? ? ?

? ? }else{

?? ? ? ?

? ? ? ?// do counter

? ? }

?

?

// 獲取前7天的數(shù)據(jù)

- (void)getStepOneWeek

{

? ? __weak ViewController * weakSelf = self; ?// 弱引用 防止內(nèi)存泄漏

? ??

? ? if ([CMPedometer isStepCountingAvailable]) { // 判斷能否計(jì)步

? ? ? ? NSMutableString * dateStr = [NSMutableString string]; //可變數(shù)組記錄每天步數(shù)

? ? ? ??

? ? ? ? for (int i = 6; i >= 0; i --) { ? // for循環(huán) 取出每天的步數(shù)

?? ? ? ? ? ?

? ? ? ? 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];

? ? ? ? ? ? // 結(jié)束日期

? ? ? ? ? ? NSDate *endTempDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startTempDate options:0];

? ? ? ? ? ? NSDate * startDate = [self getStartTimeWithDate:startTempDate]; ?// 將日期轉(zhuǎn)為某年某月某天00:00:00

? ? ? ? ? ? NSDate * endDate = [self getStartTimeWithDate:endTempDate];

? ? ? ? ? ? NSLog(@"%@? %@ ", startDate, endDate);

      // 從開(kāi)始時(shí)間到結(jié)束時(shí)間的總步數(shù)

? ? ? ? ? ? [self.pedometer queryPedometerDataFromDate:startDate toDate:endDate withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {

? ? ? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{

? ? ? ? ? ? ? ? ? ? if (error) {

? ? ? ? ? ? ? ? ? ? ? ? NSLog(@"%@", error); // 出錯(cuò) ?錯(cuò)誤信息 ?如果是Error Domain=CMErrorDomain Code=103 "The operation couldn’t be completed. (CMErrorDomain error 103.) ?去看pedometer是不是成員變量 并在viewDidiLoad:中創(chuàng)建實(shí)例

? ? ? ? ? ? ? ? ? ? ? ? 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];

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? });

? ? ? ? ? ? }];

?? ? ? ? ? ?

? ? ? ? }

? ? }

}

?

步數(shù)收集到了!

然后就是實(shí)時(shí)步數(shù)記錄

?

?

// 今天的實(shí)時(shí)數(shù)據(jù)

- (void)getTodayData

{

? ? __weak ViewController * weakSelf = self;

?? ?

? ? if ([CMPedometer isStepCountingAvailable]) {

?

? ? ? ? NSDate * date = [self getStartTime]; ?// 獲取今天的00:00:00

? ? ? ? NSLog(@"formDate:%@",date);

?

    // 從data開(kāi)始的實(shí)時(shí)步數(shù)記錄

? ? ? ? [self.pedometer startPedometerUpdatesFromDate:date withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {

? ? ? ? ? ? NSLog(@"距離%@+步數(shù)%@",pedometerData.distance, pedometerData.numberOfSteps);

? ? ? ? ? ? if (error) {

? ? ? ? ? ? ? ? NSLog(@"%@",error);

? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{

          //?distance 走的距離 ??numberOfSteps ?步數(shù)

? ? ? ? ? ? ? ? ? ? NSString * str = [NSString stringWithFormat:@"%.2f+%@",[pedometerData.distance doubleValue], pedometerData.numberOfSteps];

? ? ? ? ? ? ? ? ? ? weakSelf.distanceLabel.text = str;

? ? ? ? ? ? ? ? });

? ? ? ? ? ? }

? ? ? ? }];

?? ? ? ?

? ? }

}

?

OK 實(shí)時(shí)記錄和往日查詢都Ok了!! ?

?

Error Domain=CMErrorDomain Code=105 ?該錯(cuò)誤是因?yàn)闆](méi)有設(shè)置infoplist文件中的Motion隱私選項(xiàng) 或者是未在設(shè)置->隱私->運(yùn)動(dòng)與健康 中打開(kāi)權(quán)限。

                      另一個(gè)錯(cuò)誤code碼是104(或者103?) ?是因?yàn)?pedometer 不是property屬性(全局屬性) 。

?

?上面因?yàn)槿r(shí)間轉(zhuǎn)化問(wèn)題 可能會(huì)有步數(shù)差距。 需要根據(jù)自己的需要將時(shí)間調(diào)整好。

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/LoveStoryJX/p/6605849.html

總結(jié)

以上是生活随笔為你收集整理的CMPedometer 实现计步的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。