3

I am working on a Swift 3 application that will be running on a device indefinitely. This is highly stressful to the device and I would like to turn the display off until I trigger an event to turn it back on.

I can set isProximityMonitoringEnabled to keep the device screen off and application running in background with the sensor covered. However, the screen doesn't turn back on when I set it to false (It doesn't even turn on if I segue to another view programmatically) until I take off the cover.

UIDevice.current.isProximityMonitoringEnabled = true;

As of now, I am reducing screen brightness to 0.0 when its in idle.

UIScreen.main.brightness = CGFloat(0.0)

I do not mind using Private API's like the answer on here https://stackoverflow.com/a/12944387/1509698 says. But for the life of me I can't convert that to Swift or find a code snippet working on iOS 10.

Community
  • 1
  • 1
mimmi
  • 113
  • 1
  • 2
  • 15
  • The easiest way is probably writing an objective-c wrapper for the private api in that reference you gave and call it from swift. – ptoinson Jan 23 '17 at 09:08
  • Although, that answer is pretty old and may not work with current versions of iOS. One of the drawabacks of a private API. Other ideas: http://stackoverflow.com/questions/1679814/iphone-phone-goes-to-sleep-even-if-idletimerdisabled-is-yes/29256860#29256860 – ptoinson Jan 23 '17 at 09:15

1 Answers1

0

Try this extension, it works for me

import UIKit
extension UIScreen
{
    static func setMainBrightness(brightness: CGFloat)
    {
        guard (0...1).contains(brightness) else
        {
            print("Attempt to set the screen brightness to an invalid value: \(brightness) should be between 0 and 1 inclusive.")
            return
        }
        self.main.brightness = brightness
    }
}
Patel Jigar
  • 2,141
  • 1
  • 23
  • 30
  • 1
    Doesn't this set the brightness? I need to turn the screen off completely, then wake it up at a later time. – mimmi Jan 23 '17 at 15:44