ios - CMStepCounter not providing step count -
i'm trying explore coremotion
, i'm trying use cmstepcounter
class step counts. here how implemented 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]; }
but when walk carrying device, i'm not getting update when shake device gives me random numberofsteps
count. please let me know if i'm missing out something.
try below method. think work .call startstepcalculations
method viewdidload..i calculating steps 1 day.
- (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 total steps (today + new) self.steps += (int)numberofsteps; //todo badge number should 1000 max [[uiapplication sharedapplication] setapplicationiconbadgenumber:self.steps]; }]; }
let me know working or not.
happy coding!!!
Comments
Post a Comment