Skip to content

Instantly share code, notes, and snippets.

@djromero
Forked from bwhitman/CoreMotion.m
Last active August 29, 2015 14:05
Show Gist options
  • Save djromero/454d36efcededd5e19a2 to your computer and use it in GitHub Desktop.
Save djromero/454d36efcededd5e19a2 to your computer and use it in GitHub Desktop.
/*
2013-09-29 21:23:26.468 perf[3911:60b] Sep 26, 2013, 8:47 AM confidence high type walking steps 16
2013-09-29 21:23:26.474 perf[3911:60b] Sep 26, 2013, 8:56 AM confidence medium type walking steps 0
2013-09-29 21:23:26.492 perf[3911:60b] Sep 26, 2013, 9:08 AM confidence high type stationary steps 25
...
*/
if([CMMotionActivityManager isActivityAvailable]) {
CMMotionActivityManager *cm = [[CMMotionActivityManager alloc] init];
CMStepCounter *sc = [[CMStepCounter alloc] init];
NSDate *today = [NSDate date];
NSDate *lastWeek = [today dateByAddingTimeInterval:-(86400*7)];
[cm queryActivityStartingFromDate:lastWeek toDate:today toQueue:[NSOperationQueue mainQueue] withHandler:^(NSArray *activities, NSError *error){
for(int i=0;i<[activities count]-1;i++) {
CMMotionActivity *a = [activities objectAtIndex:i];
NSString *confidence = @"low";
if (a.confidence == CMMotionActivityConfidenceMedium) confidence = @"medium";
if (a.confidence == CMMotionActivityConfidenceHigh) confidence = @"high";
NSString *motion = @"";
if (a.stationary) motion = [motion stringByAppendingString:@"stationary "];
if (a.walking) motion = [motion stringByAppendingString:@"walking "];
if (a.running) motion = [motion stringByAppendingString:@"running "];
if (a.automotive) motion = [motion stringByAppendingString:@"automotive "];
// Now get steps as well
[sc queryStepCountStartingFrom:a.startDate to:[[activities objectAtIndex:i+1] startDate] toQueue:[NSOperationQueue mainQueue] withHandler:^(NSInteger numberOfSteps, NSError *error) {
NSLog(@"%@ confidence %@ type %@ steps %ld", [NSDateFormatter localizedStringFromDate:a.startDate dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle], confidence, motion, numberOfSteps);
}];
}
}];
} else {
NSLog(@"cm not ok");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment