- (void)startTracking {
if (!([CMStepCounter isStepCountingAvailable] || [CMMotionActivityManager isActivityAvailable])) {
NSString *msg = @"CMStepCounter and CMMotionActivityManager are not available. These classes need M7 coprocessor, so this sample works only on iPhone5s.";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not Supported"
message:msg
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
return;
}
__weak ActivityTrackingViewController *weakSelf = self;
self.operationQueue = [[NSOperationQueue alloc] init];
// Start step counting updates
if ([CMStepCounter isStepCountingAvailable])
{
self.stepCounter = [[CMStepCounter alloc] init];
[self.stepCounter startStepCountingUpdatesToQueue:self.operationQueue
updateOn:1
withHandler:
^(NSInteger numberOfSteps, NSDate *timestamp, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
[SVProgressHUD showErrorWithStatus:error.description];
}
else {
NSString *text = [NSString stringWithFormat:@"Steps: %ld", (long)numberOfSteps];
weakSelf.stepsLabel.text = text;
}
});
}];
}
// Start motion activity updates
if ([CMMotionActivityManager isActivityAvailable])
{
self.activityManager = [[CMMotionActivityManager alloc] init];
[self.activityManager startActivityUpdatesToQueue:self.operationQueue
withHandler:
^(CMMotionActivity *activity) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *status = [weakSelf statusForActivity:activity];
NSString *confidence = [weakSelf stringFromConfidence:activity.confidence];
weakSelf.statusLabel.text = [NSString stringWithFormat:@"Status: %@", status];
weakSelf.confidenceLabel.text = [NSString stringWithFormat:@"Confidence: %@", confidence];
});
}];
}
}
- (NSString *)statusForActivity:(CMMotionActivity *)activity {
NSMutableString *status = @"".mutableCopy;
if (activity.stationary) {
[status appendString:@"not moving"];
}
if (activity.walking) {
if (status.length) [status appendString:@", "];
[status appendString:@"on a walking person"];
}
if (activity.running) {
if (status.length) [status appendString:@", "];
[status appendString:@"on a running person"];
}
if (activity.automotive) {
if (status.length) [status appendString:@", "];
[status appendString:@"in a vehicle"];
}
if (activity.unknown || !status.length) {
[status appendString:@"unknown"];
}
return status;
}
- (NSString *)stringFromConfidence:(CMMotionActivityConfidence)confidence {
switch (confidence) {
case CMMotionActivityConfidenceLow:
return @"Low";
case CMMotionActivityConfidenceMedium:
return @"Medium";
case CMMotionActivityConfidenceHigh:
return @"High";
default:
return nil;
}
}