Swiftpack.co -  ochococo/Insomnia as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
๐ŸŒƒ How to prevent screen lock on my application?
.package(url: "https://github.com/ochococo/Insomnia.git", from: "2.0.1")

๐ŸŒƒ Insomnia

Insomnia Logo

Swift4.1 Coverage 100% Version License Platform Documentation Documentation ็ฎ€ไฝ“ไธญๆ–‡

โค๏ธ Please consider supporting my work, become my Sponsor! ๐Ÿ™

Micro library to answer to questions like:

  • How to prevent screen lock on my application?
  • How can I prevent the display on an iOS device from dimming and turning off?
  • How do I prevent the iPhone screen from dimming or turning off while certain ViewController is presented?

Well... Sometimes you want your iPhone to stay active a little bit longer is it an import or just game interface.

This project aims to simplify the code and give you a well tested solution.

โš™ Modes:

  • .disabled - Nothing will change (disabled functionality).
  • .always - Your iOS device will never timeout and go to sleep.
  • .whenCharging - Device will stay active as long as it's connected to charger.

Important: If Insomnia instance is deallocated, it's functionality is automatically disabled and device will behave nominally (timeout, disable screen and lock).

๐Ÿ‘จโ€๐Ÿ’ป Usage:

1. Don't go to sleep, ever:

final class AppDelegate: UIApplicationDelegate {
	private let insomnia = Insomnia(mode: .always)

2. Same but only when charging:

final class AppDelegate: UIApplicationDelegate {
	private let insomnia = Insomnia(mode: .whenCharging)

3. Don't go to sleep if certain View Controller is alive:

final class SomeViewController: UIViewController {
	private let insomnia = Insomnia(mode: .always)

4. Change mode:

insomnia.mode = .always

๐Ÿค” Rationale:

The too simple answer is:

UIApplication.shared.isIdleTimerDisabled = true

The problem with this solution is that you have to remember to change this global variable to false when it's no longer needed. What if you want to activate it only when your device is on charger?

๐Ÿ‘ Credits:

Logo design by Artur Martynowski - check his dribble profile, he's really great!


Stars: 99
Last commit: 4 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jackโ€™d and SCRUFF. We are two of the worldโ€™s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

SPM Support
4 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API