programing

iOS에서 프로그래밍 방식으로 절전 모드를 비활성화/활성화하는 방법은 무엇입니까?

showcode 2023. 6. 19. 21:48
반응형

iOS에서 프로그래밍 방식으로 절전 모드를 비활성화/활성화하는 방법은 무엇입니까?

카운트다운이 끝날 때까지 깨어 있어야 하는 앱이 있는데, 할당된 취침 시간에 도달할 때마다 '수면 모드'로 전환됩니다.

내 앱에서는 사용자가 절전 모드를 비활성화/활성화할 수 있도록 절전 모드를 연기할 수 있는 옵션이 있습니다.

프로그래밍 방식으로 어떻게 합니까?

다음과 같이 유휴 타이머를 비활성화할 수 있습니다.

목표 C:

[UIApplication sharedApplication].idleTimerDisabled = YES;

Swift에서:

UIApplication.sharedApplication().idleTimerDisabled = true

Swift 3.0 및 Swift 4.0의 경우:

UIApplication.shared.isIdleTimerDisabled = true

다시 설정NO또는false절전 모드를 다시 활성화합니다.

예를 들어 보기를 떠날 때까지 필요한 경우 Will Dispose 보기를 재정의하여 다시 설정할 수 있습니다.

override func viewWillDisappear(_ animated: Bool) {
    UIApplication.shared.isIdleTimerDisabled = false
}

UIA 응용 프로그램 클래스에 대해 자세히 알아봅니다.

Swift 3에서 유휴 타이머를 비활성화하려면 다음과 같이 하십시오.

UIApplication.shared.isIdleTimerDisabled = true

유휴 타이머를 다시 켜는 방법은 다음과 같습니다.

UIApplication.shared.isIdleTimerDisabled = false

추가로, 주의할 점은YES그리고.NOSwift에서는 사용할 수 없으며 다음 중 하나를 사용해야 합니다.true또는false(이전 답변과는 대조적으로).

iOS 13, Swift 5,5.1+로 유휴 타이머를 비활성화합니다.SceneDelegate.swift.

 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
     UIApplication.shared.isIdleTimerDisabled = true
 }

스위프트 3에서 이것이 가능한 정확한 위치는AppDelegate.swift추가해야 합니다.UIApplication.shared.isIdleTimerDisabled = true안에서.applicationfunc 결과는 다음과 같습니다.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    UIApplication.shared.isIdleTimerDisabled = true
    return true
}

언급URL : https://stackoverflow.com/questions/12661004/how-to-disable-enable-the-sleep-mode-programmatically-in-ios

반응형