iOS sensor

Use of distance sensor

//Open distance sensing

[UIDevice currentDevice].proximityMonitoringEnabled=YES;

//Monitoring distance sensing notifications

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(proximityChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];

-(void)proximityChange:(NSNotificationCenter *)notification{

if ([UIDevice currentDevice].proximityState) {

NSLog(@”An object is near Screen “); //screen will automatically lock

}else{

NSLog (@” an object away from the screen”); //screen will automatically unlock

}

}

Accelerometer

push

Collect all data in real time (high frequency of acquisition)

pull

Actively collect when needed

#import <CoreMotion/CoreMotion.h>

//accelerometer push method

//1.Create a coreMotion object

CMMotionManager *mrg=[CMMotionManager new];

//2. Is it available?

if (![mrg isAccelerometerAvailable]) {

return;

}

//3. Sampling The interval unit is seconds

mrg.accelerometerUpdateInterval=1;

//4. Start sampling

[mrg startAccelerometerUpdatesToQueue:[NSOperationQueue new] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {

NSLog(@”%@”,accelerometerData);

NSLog(@” %f—-%f—%f”,accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z);

}];

//Accelerometer pull mode

//1. Create a coreMotion object

CMMotionManager *mrg=[CMMotionManager new] ;

//2. Determine if it is available

if (![mrg isAccelerometerAvailable]) {

return;

}

//Start sampling

[mrg startAccelerometerUpdates];

NSLog(@”%f——-%f ——%f”,mrg.accelerometerData.acceleration.x,mrg.accelerometerData.acceleration.y,mrg.acc elerometerData.acceleration.z);

Gyro

//Gyro Push Mode

//1. Create a coreMotion object

CMMotionManager *mrg=[CMMotionManager new];

//2. Determine if it is available

if (![mrg isGyroAvailable]) {

return;

}

//3. The sampling interval unit is seconds

mrg.gyroUpdateInterval=1;

//4. Start sampling

[mrg startGyroUpdatesToQueue:[NSOperationQueue new] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {

NSLog(@”%f– -%f–%f”,gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z);

}];

//Gyro pull mode

//1. Create a coreMotion object

CMMotionManager *mrg=[CMMotionManager new];

//2. Available

if (![mrg isGyroAvailable]) {

return;

}

//3. Start sampling

[mrg startGyroUpdates];

NSLog(@”%f—%f–%f”,mrg.gyroData.rotationRate.x,mrg.gyroData.rotationRate.y, mrg.gyroData.rotationRate.z) ;

Magnetometer: Detecting changes in magnetic field, mainly using navigation

//Magnetic push method

//1. Create coreMotion object

CMMotionManager *mrg=[CMMotionManager new];

//2. Determine if the magnetometer is available

if (![mrg isMagnetometerAvailable]) {

return;

}

//3. Interval

mrg.magnetometerUpdateInterval=1;

//4. Start sampling

[mrg startMagnetometerUpdatesToQueue:[NSOperationQueue new] withHandler:^(CMMagnetometerData * _Nullable magnetometerData, NSError * _Nullable error) {

NSLog(@”%f- –%f–%f”,magometerometerData.magneticField.x,magometerometerData.magneticField.y,magometerometer.magneticField.z);

}];

//Magnetometer pull mode

//1. Create a coreMotion object

CMMotionManager *mrg=[CMMotionManager new];

//2. Determine if the magnetometer is available

if (![mrg isMagnetometerAvailable]) {

return;

}

//3. Start Sampling

[mrg startMagnetometerUpdates];

NSLog(@” %f—%f–%f”,mrg.magnetometerData.magneticField.x,mrg.magnetometerData.magneticField.y,mrg.magnetometerData.magneticField.z);

Shake it

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

NSLog(@”shake”);

}

-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{

NSLog(@”Unshake”);

}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

NSLog(@”Shake End” );

}

Pedometer

//1. iOS8 pedometer determines if it is available

if (![ CMPedometer isStepCountingAvailable]) {

return;

}

//2. Create a step-by-step class

CMPedometer *ped= [CMPedometer New];

//3. Start step counting statistics

[ped startPedometerUpdatesFromDate:[NSDate date] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {

//4.Steps

NSNumber *number= pedometerData.numberOfSteps;

NSLog(@”%@”,number);

}];

iOS Sensor