//.首先ios7以上的系统 有运动协处理器的设备 用户必须是允许读取健身数据的前提下
if ([CMStepCounter isStepCountingAvailable]) {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
_dateNow = now ;
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
NSDate *startDate = [calendar dateFromComponents:components];
NSDate *endDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startDate options:0];
[self.stepCounter queryStepCountStartingFrom:startDate to:endDate toQueue:self.operationQueue withHandler:^(NSInteger numberOfSteps, NSError * _Nullable error) {
NSString *text = [NSString stringWithFormat:@"步數: %ld", (long)numberOfSteps];
//weakSelf.bu.text = (long)numberOfSteps ;
weakSelf.bu.text = [NSString stringWithFormat:@"%ld", (long)numberOfSteps];
weakSelf.bushu = [NSString stringWithFormat:@"%ld", (long)numberOfSteps];
// [self showHint:[NSString stringWithFormat:@"%ld", (long)numberOfSteps]];
//weakSelf.stepsLabel.text = text;
}];
self.stepCounter = [[CMStepCounter alloc] init];
[self.stepCounter startStepCountingUpdatesToQueue:self.operationQueue
updateOn:20
withHandler:
^(NSInteger numberOfSteps, NSDate *timestamp, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请允许资源家访问运动与健康" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[error show];
}
else {
[self.stepCounter queryStepCountStartingFrom:startDate to:endDate toQueue:self.operationQueue withHandler:^(NSInteger numberOfSteps, NSError * _Nullable error) {
NSString *text = [NSString stringWithFormat:@"步數: %ld", (long)numberOfSteps];
//weakSelf.bu.text = (long)numberOfSteps ;
weakSelf.bu.text = [NSString stringWithFormat:@"%ld", (long)numberOfSteps];
weakSelf.bushu = [NSString stringWithFormat:@"%ld", (long)numberOfSteps];
// [self showHint:[NSString stringWithFormat:@"%ld", (long)numberOfSteps]];
//weakSelf.stepsLabel.text = text;
}];
//
// NSString *text = [NSString stringWithFormat:@"步數: %ld", weakSelf.numBu +(long)numberOfSteps];
// NSDate *nowDa = [NSDate date];
// NSString *text1 = [NSString stringWithFormat:@"%ld", weakSelf.numBu +(long)numberOfSteps];
// self.bu.text = text1 ;
// self.bushu = text1 ;
// [self showHint:text];
}
});
}];
}
//開波
if ([CMMotionActivityManager isActivityAvailable]) {
self.activityManager = [[CMMotionActivityManager alloc] init];
[self.activityManager startActivityUpdatesToQueue:self.operationQueue
withHandler:
^(CMMotionActivity *activity) {
dispatch_async(dispatch_get_main_queue(), ^{
});
}];
}
//5S 以下的设备 需要自己太高算法去计算
NSString * st =[[NSUserDefaults standardUserDefaults]objectForKey:@"bushu"];
stepCount = st.intValue ;
__weak FirstViewController *weakSelf = self;
[SOMotionDetector sharedInstance].motionTypeChangedBlock = ^(SOMotionType motionType) {
NSString *type = @"";
switch (motionType) {
case MotionTypeNotMoving:
type = @"Not moving";
break;
case MotionTypeWalking:
type = @"Walking";
break;
case MotionTypeRunning:
type = @"Running";
break;
case MotionTypeAutomotive:
type = @"Automotive";
break;
}
};
[SOMotionDetector sharedInstance].locationChangedBlock = ^(CLLocation *location) {
};
[SOMotionDetector sharedInstance].accelerationChangedBlock = ^(CMAcceleration acceleration) {
};
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
[SOMotionDetector sharedInstance].useM7IfAvailable = YES; //Use M7 chip if available, otherwise use lib's algorithm
}
[[SOMotionDetector sharedInstance] startDetection];
[[SOStepDetector sharedInstance] startDetectionWithUpdateBlock:^(NSError *error) {
if (error) {
NSLog(@"%@", error.localizedDescription);
return;
}
NSDate *nowD = [NSDate date];
stepCount++;
}
网友评论