計步器: iOS7開始可使用CMStepCounter獲取用戶的走路步數, iOS8更新了健康應用後, 開始使用CMPedometer獲取用戶的走路步數/距離/樓層等信息,具體代碼以下:web
- (void)viewDidLoad {dom
[super viewDidLoad];spa
//1. 判斷是否可用3d
if (![CMPedometer isStepCountingAvailable]) {orm
return;string
}it
//2. 建立計步器form
self.pedometer = [CMPedometer new];date
//3. 開始統計數據select
[self.pedometer startPedometerUpdatesFromDate:[NSDate date] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
//4. 讓界面發生改變
[self performSelectorOnMainThread:@selector(updateUI:) withObject:pedometerData.numberOfSteps waitUntilDone:YES];
NSLog(@"number: %@",pedometerData.numberOfSteps);
}];
}
- (void)updateUI:(NSNumber *)numberOfSteps
{
self.label.text = [NSString stringWithFormat:@"您當前一共走了%@步..",numberOfSteps];
}