有关ios健康记步的算法

//.首先ios7以上的系统  有运动协处理器的设备  用户必须是允许读取健身数据的前提下

 if ([CMStepCounter isStepCountingAvailable]) {

        NSCalendar *calendar = [NSCalendar currentCalendar];

        

        NSDate *now = [NSDate date];

        _dateNow = now ;

        NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];

        

        NSDate *startDate = [calendar dateFromComponents:components];

        

        NSDate *endDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startDate options:0];

        [self.stepCounter queryStepCountStartingFrom:startDate to:endDate toQueue:self.operationQueue withHandler:^(NSInteger numberOfSteps, NSError * _Nullable error) {

            NSString *text = [NSString stringWithFormat:@"步數: %ld", (long)numberOfSteps];

            //weakSelf.bu.text = (long)numberOfSteps ;

            weakSelf.bu.text =   [NSString stringWithFormat:@"%ld", (long)numberOfSteps];

            weakSelf.bushu = [NSString stringWithFormat:@"%ld", (long)numberOfSteps];

            //  [self showHint:[NSString stringWithFormat:@"%ld", (long)numberOfSteps]];

            //weakSelf.stepsLabel.text = text;

        }];

        self.stepCounter = [[CMStepCounter alloc] init];

               [self.stepCounter startStepCountingUpdatesToQueue:self.operationQueue

                                                 updateOn:20

                                              withHandler:

         ^(NSInteger numberOfSteps, NSDate *timestamp, NSError *error) {

             

             dispatch_async(dispatch_get_main_queue(), ^{

                 

                 if (error) {

                     UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请允许资源家访问运动与健康" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

                     [error show];

                 }

                 else {

                     [self.stepCounter queryStepCountStartingFrom:startDate to:endDate toQueue:self.operationQueue withHandler:^(NSInteger numberOfSteps, NSError * _Nullable error) {

                         NSString *text = [NSString stringWithFormat:@"步數: %ld", (long)numberOfSteps];

                         //weakSelf.bu.text = (long)numberOfSteps ;

                        weakSelf.bu.text =   [NSString stringWithFormat:@"%ld", (long)numberOfSteps];

                        weakSelf.bushu = [NSString stringWithFormat:@"%ld", (long)numberOfSteps];

                       //  [self showHint:[NSString stringWithFormat:@"%ld", (long)numberOfSteps]];

                         //weakSelf.stepsLabel.text = text;

                     }];

//

//                     NSString *text = [NSString stringWithFormat:@"步數: %ld", weakSelf.numBu +(long)numberOfSteps];

//                     NSDate *nowDa = [NSDate date];

//                     NSString *text1 = [NSString stringWithFormat:@"%ld", weakSelf.numBu +(long)numberOfSteps];

//                     self.bu.text = text1 ;

//                     self.bushu = text1 ;

                     // [self showHint:text];

                     

                     

                 }

             });

         }];

    }

    

    //開波

    if ([CMMotionActivityManager isActivityAvailable]) {

        

        self.activityManager = [[CMMotionActivityManager alloc] init];

        

        [self.activityManager startActivityUpdatesToQueue:self.operationQueue

                                              withHandler:

         ^(CMMotionActivity *activity) {

             

             dispatch_async(dispatch_get_main_queue(), ^{

                 

             });

         }];

    }

    //5S  以下的设备 需要自己太高算法去计算   

NSString * st =[[NSUserDefaults standardUserDefaults]objectForKey:@"bushu"];

    stepCount = st.intValue ;

    __weak FirstViewController *weakSelf = self;

    [SOMotionDetector sharedInstance].motionTypeChangedBlock = ^(SOMotionType motionType) {

        NSString *type = @"";

        switch (motionType) {

            case MotionTypeNotMoving:

                type = @"Not moving";

                break;

            case MotionTypeWalking:

                type = @"Walking";

                break;

            case MotionTypeRunning:

                type = @"Running";

                break;

            case MotionTypeAutomotive:

                type = @"Automotive";

                break;

        }

    

    };

    

    [SOMotionDetector sharedInstance].locationChangedBlock = ^(CLLocation *location) {

   

    };

    

    [SOMotionDetector sharedInstance].accelerationChangedBlock = ^(CMAcceleration acceleration) {


    };

    

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {

        [SOMotionDetector sharedInstance].useM7IfAvailable = YES; //Use M7 chip if available, otherwise use lib's algorithm

    }

    

    [[SOMotionDetector sharedInstance] startDetection];

    [[SOStepDetector sharedInstance] startDetectionWithUpdateBlock:^(NSError *error) {

        if (error) {

            NSLog(@"%@", error.localizedDescription);

            return;

        }

        NSDate *nowD = [NSDate date];


            stepCount++;


}

    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值