How to use proximity sensor programmatically with iOS development?

时间秒杀一切 提交于 2019-12-06 20:49:04

问题


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:


The screen lock can be enabled/disabled.

[UIApplication sharedApplication].idleTimerDisabled = YES;



来源:https://stackoverflow.com/questions/31270887/how-to-use-proximity-sensor-programmatically-with-ios-development

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!