【问题标题】:CMStepCounter not providing step countCMStepCounter 不提供步数
【发布时间】:2014-04-21 11:44:49
【问题描述】:

我正在尝试探索 CoreMotion 并且我正在尝试使用 CMStepCounter 类来获取步数。这是我如何实现我的view controller 来获得stepCounts

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *stepsCountingLabel;  
@property (nonatomic, strong) CMStepCounter *cmStepCounter;
@property (nonatomic, strong) NSOperationQueue *operationQueue;

@end

@implementation ViewController

- (NSOperationQueue *)operationQueue
{
    if (_operationQueue == nil)
    {
        _operationQueue = [NSOperationQueue new];
    }
    return _operationQueue;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    if ([CMStepCounter isStepCountingAvailable])
    {
        self.cmStepCounter = [[CMStepCounter alloc] init];
        [self.cmStepCounter startStepCountingUpdatesToQueue:self.operationQueue updateOn:1 withHandler:^(NSInteger numberOfSteps, NSDate *timestamp, NSError *error)
         {
             [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                 [self updateStepCounterLabelWithStepCounter:numberOfSteps];
             }];
         }];
    }
    else
    {
        self.stepsCountingLabel.text = @"NO STEP COUNTING AVAILABLE";
    }
}

- (void)updateStepCounterLabelWithStepCounter:(NSInteger)countedSteps
{
    self.stepsCountingLabel.text = [NSString stringWithFormat:@"%ld", (long)countedSteps];
}

但是当我带着设备走路时,我没有得到任何更新,但是当我摇动设备时,它给了我一些随机的numberOfSteps 计数。如果我错过了什么,请告诉我。

【问题讨论】:

    标签: ios iphone ios7 core-motion


    【解决方案1】:

    试试下面的方法。我认为它会起作用。从 viewdidload 调用 startStepCalculations 方法。我正在计算 1 天的步数。

    - (void)startStepCalculations {
     if (!self.stepCounter) {
        self.stepCounter = [[CMStepCounter alloc] init];
      }
      if (![CMStepCounter isStepCountingAvailable]) {
        return;
      }
     NSCalendar *cal = [NSCalendar currentCalendar];
      NSDateComponents *components = [cal components:(NSHourCalendarUnit |
                                                      NSMinuteCalendarUnit |
                                                  NSSecondCalendarUnit)
                                        fromDate:[[NSDate alloc] init]];
    
       [components setHour:-[components hour]];
       [components setMinute:-[components minute]];
       [components setSecond:-[components second]];
        NSDate *start = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0];
    
      [components setHour:+24];
      [components setMinute:0];
      [components setSecond:0];
      NSDate *end = [cal dateByAddingComponents:components toDate:start options:0];
    
      NSOperationQueue *queue = [NSOperationQueue mainQueue];
      [self.stepCounter queryStepCountStartingFrom:start
                                            to:end
                                       toQueue:queue
                                   withHandler:^(NSInteger numberOfSteps, NSError *error) {
       if (error) {
         NSLog(@"ERROR : %@",error.localizedDescription);
         return;
       }
       self.steps = numberOfSteps;
       [[UIApplication sharedApplication] setApplicationIconBadgeNumber:self.steps];
       [self beginMonitoringForNewSteps];
      }];
    }
    
    
     - (void)beginMonitoringForNewSteps {
      if (!self.stepCounter) {
       self.stepCounter = [[CMStepCounter alloc] init];
      }
     NSOperationQueue *queue = [NSOperationQueue mainQueue];
      [self.stepCounter startStepCountingUpdatesToQueue:queue
                                           updateOn:1
                                        withHandler:^(NSInteger numberOfSteps, NSDate *timestamp, NSError *error) {
    // calculate the total steps (today + new)
       self.steps += (int)numberOfSteps;
     //TODO badge number should be 1000 max
         [[UIApplication sharedApplication] setApplicationIconBadgeNumber:self.steps];
        }];
    }
    

    让我知道它是否有效。

    编码愉快!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-05
      • 1970-01-01
      • 2014-01-02
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多