How to prevent the screen from sleeping in iOS?

Last updated Oct 22, 2021

In this ios example code we will cover how to prevent screen from sleeping in iOS. To prevent screen sleeaping we will set application.idleTimerDisabled property true

application.idleTimerDisabled = true

 

So, let’s get started.

Step 1: Create simple Ios application in xcode IDE

Step 2: Add required code in app delegete

 

When you need in appdelegate,

import UIKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

         application.idleTimerDisabled = true

         return true

}

 

Outside AppDelegate,

import UIKit

override func viewDidLoad() {  

 super.viewDidLoad()  

          // Do any additional setup after loading the view, typically from a nib.  

          UIApplication.shared.isIdleTimerDisabled = true

}

 

Using Objective C:

       

  [[UIApplication sharedApplication]setIdleTimerDisabled:YES];                 

  

 

Step 3:

Run the application, the application will never go in sleep mode.

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

254 Views