ios - CMStepCounter 不提供步数

标签 ios iphone ios7 core-motion

我正在尝试探索 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 计数。如果我遗漏了什么,请告诉我。

最佳答案

试试下面的方法。我认为它会起作用。从 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];
    }];
}

让我知道它是否有效。

编码愉快!!!

关于ios - CMStepCounter 不提供步数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23196765/

相关文章:

iphone - NSDecimalNumber 和从末尾弹出数字

iphone - SQLite Select 语句的更好性能

iphone - 后台运行的 iOS 应用程序

ios - UITextField,在缩放模式下聚焦时文本模糊

ios - 将 double 类型的数组元素相乘

ios - 如何更改 UINavigationItem 的 leftBarButtonItem 的大小

iphone - UISearchBar 与 iOS7 中 UITableView header 中的 UISearchDisplayController 链接

ios - 如何将 ios-chart 限制为 View 大小

iphone - 检测iPad应用程序中是否连接了蓝牙设备

javascript - iOS 7 javascript XMLHttpRequest 错误