Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use proximity sensor programmatically with iOS development?

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!!

like image 233
Confused Avatar asked Jul 07 '15 14:07

Confused


1 Answers

The screen lock can be enabled/disabled.

[UIApplication sharedApplication].idleTimerDisabled = YES;
like image 93
Templar Avatar answered Sep 19 '22 15:09

Templar