11

After some googling, I can understand that the "proximity sensor" which is used to on/off screen when the device is away/near from the user. I watched this video (watch from 30th sec) and surprised about this cool stuff. I want to implement it in my app.

But I come to know that there is no public API is available that can protect the screen lock when proximityMonitoringEnabled is YES. Then how can the above app did this?

For clear understanding, I'm copying some code.

Enable the proximity sensor:

[[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; 

Setup an observer for sensor change:

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

Finally you can find the state of 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 view when the "Device is close to user" state was called. And want to remove the view if "Device is not closer to user" state was called.

So I added a view and removed inside the sensorStateMonitor: method. But the view was visible only for some fraction of seconds and the screen goes off.

Can I prevent the screen from auto off?

Just confused!!

1
  • Hi @Confused, how can i prevent the screen form auto off is there any solution ? if yes , can u plz help me on this . Commented Nov 17, 2016 at 11:18

1 Answer 1

2

The screen lock can be enabled/disabled.

[UIApplication sharedApplication].idleTimerDisabled = YES; 
Sign up to request clarification or add additional context in comments.

3 Comments

Even including this code next to NSLog(@"Device is close to user."); does not works. Still the screen gets locked automatically.
It seems that there might be a bug with this. Try to set it in applicationDidFinishLaunching or after [[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; first to NO, then to YES. Check this link : iosskill.blogspot.ro/2013/08/…
I tried it before my previous comment itself. That too wont help :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.