How to prevent screen lock on my application with swift on iOS

Use this:

Objective-C:

[[UIApplication sharedApplication] setIdleTimerDisabled: YES];

Swift (legacy):

UIApplication.sharedApplication().idleTimerDisabled = true

Swift 3 and above:

UIApplication.shared.isIdleTimerDisabled = true

Make sure to import UIKit.

Here is the link to the documentation from developer.apple.com.


For Swift 3.0 here are two options depending on where you want to invoke the code:

Inside AppDelegate.swift:

application.idleTimerDisabled = true

Outside AppDelegate.swift:

UIApplication.shared().isIdleTimerDisabled = true