سؤال

In android if you hold a partial wake lock, the CPU will continue to run, regardless of any display timeouts or the state of the screen and even after the user presses the power button. In all other wake locks, the CPU will run, but the user can still put the device to sleep using the power button. Is there any such mechanism in iOS like this? For CPU will continue to run and detecting the gestures and Sensors while device is in lock screen.

هل كانت مفيدة؟

المحلول

No. This is deliberately not available and hidden by Apple to conserve battery life. The only background modes to allow this, where your app is basically always executing in the background and the device does not go to sleep (but screen can be off), are VOIP and location services. To prevent abuse of these background modes, Apple reviewers are much harder on apps that specify these modes in their plist file, to make sure the apps really require these modes.

If you intend to make an enterprise app, which will not go to the app store, you can of course use these modes, as well as private API, to achieve what you wish.

نصائح أخرى

For what its worth,

when device camera is on preview, screen wont go to sleep.

maybe a usable WAKELOCK-like option for immediate use.

If anyone is still searching, I found a way to prevent the app from going to sleep by setting the isIdleTimerDisabled to true. It turns off the timer that detects the time when the phone should go to sleep.

UIApplication.shared.isIdleTimerDisabled = true

Now if you want to turn it off at a specific time point you could also try to use a timer to set it to false again. For example if you want to turn it off after 3 minutes, you could do something like that:

let _ = Timer.scheduledTimer(withTimeInterval: 180, repeats: false) { (Timer) in
    UIApplication.shared.isIdleTimerDisabled = false
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top