简体   繁体   中英

How to turn iOS device display completely off?

We are currently developing an iOS application which we need to run in Single App Mode with device lock deactivated. What we would like to have is that the screen turns completely dark and as soon as there is a socket/mqtt event or a touch event the display should brighten up.

Everything works so far the only thing that is not perfect is that the display doesn't turn completely off using:

UIScreen.main.brightness = CGFloat(0.0)

Is there a way on how we could achieve that?

Unfortunately there is no current way to do it.

Without a jailbreak there is no actual way to turn of the screen. And even mimmiking the screen turn off like you are doing now will propably get your app rejected.

The reason is that even if the pixel is black, the screen backlight is still on in LED displays.

PS: The new iPhone 8 is said to come with OLED display (if it turned out to be true). The oled screen shuts down the light of the pixel when it is black unlike the current screen (OLED have independednt light for each pixel unlike LED that's why it turns off). Thus your code will work perfectly on iphone 8. This is a hardware change not a software change thus you cant adapt it in your code.

Hope this helps!

If you want to achieve "locked" screen effect, I would recommend adding UIView as subview of your UIViewController that will take whole screen. That UIView should have black background and minimum brightness set. When you get event from the socket, just animate UIView disappearing with setting brightness to desired constant.

Example when you get event:

overlayView.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
vc.addSubview(overlayView)

UIView.animate(withDuration: 0.25, delay: 0.0, options: [], animations: {
    self.overlayView?.removeFromSuperview

}, completion: { (finished: Bool) in
    UIScreen.main.brightness = CGFloat(0.8)
})

I'm writing this answer without helping of compiler so please do not mind me if there's some syntax error. This is only draft.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM