有关ios健康记步的算法

来源:互联网 发布:mac电脑还原出厂设置 编辑:程序博客网 时间:2024/06/15 20:42

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

 if ([CMStepCounterisStepCountingAvailable]) {

       NSCalendar *calendar = [NSCalendarcurrentCalendar];

        

       NSDate *now = [NSDatedate];

       _dateNow = now ;

       NSDateComponents *components = [calendarcomponents:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDayfromDate:now];

        

       NSDate *startDate = [calendar dateFromComponents:components];

        

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

        [self.stepCounterqueryStepCountStartingFrom:startDate to:endDate toQueue:self.operationQueuewithHandler:^(NSInteger numberOfSteps,NSError * _Nullable error) {

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

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

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

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

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

           //weakSelf.stepsLabel.text = text;

        }];

       self.stepCounter = [[CMStepCounteralloc] init];

               [self.stepCounterstartStepCountingUpdatesToQueue:self.operationQueue

                                                updateOn:20

                                             withHandler:

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

             

            dispatch_async(dispatch_get_main_queue(), ^{

                 

                if (error) {

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

                     [errorshow];

                 }

                else {

                     [self.stepCounterqueryStepCountStartingFrom:startDate to:endDate toQueue:self.operationQueuewithHandler:^(NSInteger numberOfSteps,NSError * _Nullable error) {

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

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

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

                        weakSelf.bushu = [NSStringstringWithFormat:@"%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 ([CMMotionActivityManagerisActivityAvailable]) {

        

       self.activityManager = [[CMMotionActivityManageralloc] init];

        

        [self.activityManagerstartActivityUpdatesToQueue:self.operationQueue

                                             withHandler:

         ^(CMMotionActivity *activity) {

             

            dispatch_async(dispatch_get_main_queue(), ^{

                 

             });

         }];

    }

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

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

   stepCount = st.intValue ;

   __weak FirstViewController *weakSelf =self;

    [SOMotionDetectorsharedInstance].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;

        }

    

    };

    

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

   

    };

    

    [SOMotionDetectorsharedInstance].accelerationChangedBlock = ^(CMAcceleration acceleration) {


    };

    

   if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {

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

    }

    

    [[SOMotionDetectorsharedInstance] startDetection];

    [[SOStepDetectorsharedInstance] startDetectionWithUpdateBlock:^(NSError *error) {

       if (error) {

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

           return;

        }

       NSDate *nowD = [NSDatedate];


           stepCount++;


}

    


0 0
原创粉丝点击