JoeC
JoeC

Reputation: 15

IOS Proximity sensor putting screen to sleep permanently

I'm a newbie to IOS programming, but my job requires me to build apps for TV actors to use on set. I'm learning Swift. The apps are not intended for the app store. I'm working on a fake phone app. The producers want the phone to turn off when the actor puts it to their face - I have this code:

//PROXIMITY SENSOR
func proximityChanged(notification: NSNotification) {
    if let device = notification.object as? UIDevice {
        println("\(device) detected!")

    }
}

func activateProximitySensor() {
    let device = UIDevice.currentDevice()
    device.proximityMonitoringEnabled = true
    if device.proximityMonitoringEnabled {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "proximityChanged:", name: "UIDeviceProximityStateDidChangeNotification", object: device)
    }
}

It works, but the problem I have is when the proximity sensor is engaged for too long, say the phone is in the actors pocket for 2 minutes- when he goes to use it - the screen stays black.

It will wake up if the home button is pressed, but that causes it to jump out of the app. It will also wake up if the side silent/sound toggle is activated.

I'm guessing that maybe even though the app is in the foreground, somehow the proximity sensor being engaged for so long causes it to go to the background. ts hard to test in xcode, because it doesn't go to sleep when tethered to the computer. Any ideas what's causing this and how to fix it? Thanks

Upvotes: 1

Views: 701

Answers (1)

Warpling
Warpling

Reputation: 2115

Neat job! I've seen this happen before. Have you tried disabling the idle timer?

[UIApplication sharedApplication].idleTimerDisabled = YES;

Upvotes: 0

Related Questions