How to use proximity sensor programmatically with iOS development?

After some searches, I can understand that the “proximity sensor”, which is used to turn on / off the screen when the device is away from the user. I watched this video (watch from the 30th second) and was surprised at this cool stuff. I want to implement it in my application.

But I found out that the public API is not available, which can protect the screen lock when proximityMonitoringEnabled is YES . Then how can this make the application higher?

For a clear understanding, I am copying the code.

Turn on proximity sensor:

 [[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; 

Setting the observer to change the sensor:

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

Finally, you can find the status of the proximity sensor from this method:

 - (void)sensorStateMonitor:(NSNotificationCenter *)notification { if ([[UIDevice currentDevice] proximityState] == YES) { NSLog(@"Device is close to user."); } else { NSLog(@"Device is not closer to user."); } } 

Question:

I want to show some idea when the state “Device close to user” was triggered. And you want to delete the view if the status "Device is not closer to the user" was called up.

So, I added a view and deleted inside the sensorStateMonitor: method. But the view was visible only for a fraction of a second, and the screen goes blank.

Can I prevent the screen from turning off automatically?

Just confused !!

+6
source share
1 answer

Screen lock can be enabled / disabled.

[UIApplication sharedApplication].idleTimerDisabled = YES;

+1
source

All Articles