How to Prevent Screen Lock on My Application with Swift on iOS

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.

How to disable iPhone/iPad auto-lock while app is in foreground mode?

Enable the idle timer in

- (void)applicationWillResignActive:(UIApplication *)application

and disable it in

- (void)applicationDidBecomeActive:(UIApplication *)application

Disable automatic screen lock in iOS 5.1

Just setting [UIApplication sharedApplication].idleTimerDisabled = YES; in

- (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

works well for me. However, there is a caveat. I have noticed that every time I invoke camera utility to take a snapshot, idleTimerDisable gets set to NO behind the scene. So right after I upload my image, I had to call the following line of code again:

[UIApplication sharedApplication].idleTimerDisabled = YES;

I would not be surprised if there are more places throughout that require same strategy. So far this approach has worked without issues for me.

How do I prevent Interface Controller from locking screen? (WatchOS)

No watch app, independent or otherwise, can use UIKit. watchOS apps use WatchKit.

There is no direct equivalent to isIdleTimerDisabled due to the more limited battery life of watches.

If your app is a "self care" or "mindfulness" app then you can use an extended runtime session to remain as the front most app for 10 minutes or 1 hour.

If it is a fitness app then you can use an HKWorkoutSession to be the default front most app when the user raises their wrist during the workout.

iOS 8 run app forever or disable screen

Try the UIApplicationExitsOnSuspend solution:

Since iOS 4 apps get suspended when you hit the home button. Before this (iOS 3) they would exit and relaunch every time you opened the app - however if you didn't use the home button to close the app but instead locked the device with your iOS 3 app open it would run in the background forever. You can get your apps to work like this by adding the UIApplicationExitsOnSuspend key to your info plist, set the value to YES.

You wont be able to close and reopen the app as fast (it will relaunch every time you use the home button), but if you don't interact with the iPad very often, then you can just lock the device while the app is open and it will continue to run until the iPad is unlocked or you open another app.

Note that this solution is valid for the App Store, as it uses official Apple APIs.

https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html



Related Topics



Leave a reply



Submit