找到你要的答案

Q:IOS Proximity sensor putting screen to sleep permanently

Q:iOS接近传感器把屏幕睡觉,永久

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

我是一个新手,IOS编程,但我的工作要求我对电视演员使用上构建应用程序。我在学习斯威夫特。该应用程序不打算为应用程序商店。我正在做一个假的手机应用程序。制片人希望手机关掉当演员把它放在他们的脸-我有这个代码:

//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)
    }
}

它的工作原理,但我的问题是当接近传感器订婚太久,说手机是在演员口袋里2分钟-当他去使用它-屏幕保持黑色。

它会唤醒,如果按下Home键,但这会导致它跳出了应用程序。它也会唤醒,如果侧沉默/声音切换激活。

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

answer1: 回答1:

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

[UIApplication sharedApplication].idleTimerDisabled = YES;

整洁的工作!我以前见过这种情况。你试过禁用空闲计时器吗?

[UIApplication sharedApplication].idleTimerDisabled = YES;
ios  background  sleep  proximity  proximitysensor