16

I am using this in my appdelegate's applicationDidFinishLaunching: method to make sure the iPhone doesn't go to sleep during the time the app is open

[application setIdleTimerDisabled:YES];

It works great on all screens but on one of the screens the iPhone goes to sleep. I could not figure out how to reproduce this and it seems to happen at random times.

Can someone please tell me how to handle this situation.

Thanks

lostInTransit
  • 70,519
  • 61
  • 198
  • 274

9 Answers9

13

I set and un-set this property throughout my app using:

[UIApplication sharedApplication].idleTimerDisabled = YES;

Setting this where you're having trouble could fix it, though it might be a bit of a band-aid solution.

Kenny Winker
  • 11,919
  • 7
  • 56
  • 78
12

Thankfully, this question was posted 5 years ago, and Xcode has made leaps and bounds of progress since then.

However... this bug is still alive and kicking in 2015, using Xcode 6.2 and iOS 8.2.

Here's what you actually need to do, to prevent your device from going to sleep.

(Grab a beer, this is painful.)

When my app was first run on a device, it would load a bunch of data from a web service. However, if it took too long to run, the screen would fade, then turn off, the device would lock, and my web request would terminate with an ugly "The network connection was lost" error.

I attempted to add the code to simply set/unset the idleTimerDisabled value, but this change didn't last very long, and the device would still auto-lock after a while.

//  This didn't work for me  (for very long !)
[UIApplication sharedApplication].idleTimerDisabled = NO;
[UIApplication sharedApplication].idleTimerDisabled = YES;

Instead, what I needed to do (depressed sigh..) was to have a timer set/unset this value every 20 seconds.

In my .h file:

@property (strong, nonatomic) NSTimer* stayAliveTimer;
-(void)callEveryTwentySeconds;

In my .m file:

-(void)callEveryTwentySeconds
{
    //  DON'T let the device go to sleep during our sync
    [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}

-(void)loadDataFromWebService
{
    self.stayAliveTimer = [NSTimer scheduledTimerWithTimeInterval:20.0
                                                           target:self
                                                         selector:@selector(callEveryTwentySeconds)
                                                         userInfo:nil
                                                          repeats:YES];

    //  
    //  Code to call our web service in a background thread and wait
    //  for it to finish (which might take a few minutes)
    //  

    //  Kill off our "Stay alive" timer, and allow the device to Auto Lock whenever it wants.
    [self.stayAliveTimer invalidate];

    //  Give our device permission to Auto-Lock when it wants to again.
    [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}

Really, Apple ?

It's 2015, and Xcode is really still this bad...?

I hope this code helps other Xcode victims.

Tricertops
  • 8,492
  • 1
  • 39
  • 41
Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
  • 4
    It’s called _Xcode_ and this is really not a problem of the IDE. Target your anger properly. – Tricertops Jun 04 '15 at 09:43
  • 4
    My apologies to everyone for my incorrect capital letter in the name Xcode. I feel so foolish. – Mike Gledhill Jun 04 '15 at 12:53
  • Why do we need to set/unset the timer in every 20 seconds? Probably it should not be the perfect solution as I'm also having the same problem and want to fix it. I'm setting the `idleTimerDisabled` to `YES` but though my screen is dimming after a while. – if-else-switch Oct 12 '15 at 08:13
  • It certainly isn't the perfect solution. But the problem is, if you don't "set it to YES, and set it to NO" every so often, the device does go to sleep. So you need some kind of loop in there, to keep your device awake.. If you can find a better way, do, please let us know ! – Mike Gledhill Oct 12 '15 at 08:21
  • Definitely there is something wrong with idleTimerDisabled. I have these issues: On my 5s it works setting to YES, but setting then to NO does not enable sleep. Also, some users complaints that it does not work on their devices at all - it goes to sleep anyway. Looks like it's behavior depends on device. What iPhone do you use? – AlexeyVMP Nov 20 '15 at 05:51
  • I wrote this code for an in-house app, which is run on iPads, iPhone 5S and iPhone 6 devices. It has worked fine on all of them. – Mike Gledhill Nov 20 '15 at 07:31
  • I'm currently also experiencing this issue but unfortunately can't find a way to reproduce it. Sometimes the disabling the idle timer works.. and sometimes it doesn't. This happened on an iPhone 6s. – Sender Nov 20 '15 at 12:16
  • I have an iPad 4th gen I test on and the device stays on for over 24 hours but other devices iPad air 2 which are out in production still fall asleep, is there any resolution to this? – JMStudios.jrichardson Apr 02 '16 at 00:48
  • Anyone confirm if this is still an issue with iOS 9+? – stonedauwg Jun 28 '16 at 21:55
  • It is to confirm that this still works in iOS 14.3 and this is the only solution that works, This should be an accepted answer. – Dhaval Panchal Jan 15 '21 at 13:16
10

try to use

[UIApplication sharedApplication].idleTimerDisabled = NO;
[UIApplication sharedApplication].idleTimerDisabled = YES;

instead of

[UIApplication sharedApplication].idleTimerDisabled = YES;

funny hack but it works

see also the thread idleTimerDisabled not working since iPhone 3.0

Community
  • 1
  • 1
Valera
  • 111
  • 1
  • 5
6

If your application is using camera then app will go to sleep after using camera . So you need to add UIRequiredDeviceCapabilities in your app plist . Different devices you can add as shown here

Kannan Prasad
  • 1,796
  • 22
  • 27
  • Do you have further information about this camera thing? blog/article or something? because we can't really add UIRequiredDeviceCapabilities for camera as it is not required to use our app. – Buju Jul 18 '12 at 15:20
  • 1
    @Buju In my app I found that after using the camera the `idleTimer` setting are overwritten by the **camera app**. After specifying the `UIRequiredDeviceCapabilities` issue is fixed . Please look into http://blog.manbolo.com/2012/05/02/how-to-indicate-what-devices-are-supported-by-your-ios-app. – Kannan Prasad Jul 20 '12 at 04:26
  • Can't we just set the idleTimer disabled after returning from the camera picker? I'm going to try that. – Andrew Smith Dec 07 '13 at 02:59
3

Instead of Appdelegate, It is better to use it in a specific view controller in which you want screen light to be alive.

Swift 5.0 Version:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    UIApplication.shared.isIdleTimerDisabled = true
}
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.shared.isIdleTimerDisabled = false
}
Joga singh
  • 74
  • 1
  • 9
1

Set this property in the application delegate's +initialize method, e.g.:

+ (void) initialize {   
    if ([self class] == [MyAppDelegate class]) {
        UIApplication* myApp = [UIApplication sharedApplication];
        myApp.idleTimerDisabled = YES;
    }
}
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
1

I had a similar problem, and rebooting the phone fixed it. I couldn't find a programmatic fix for it, as even timers calling the idleTimerDisabled function on a one second interval didn't fix it either.

Kalen
  • 3,106
  • 8
  • 29
  • 42
1

i had the same problem. i need to make the screen dim, but when turn off the camera, the iphone goes to sleep.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}

Then i call it after turn on/off the camera, it works.

[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
kebo
  • 56
  • 3
-3

I think the setIdleTimerDisabled doesn't exist in UIApplication class. Property searches not only set- method but also other similar named methods.

Use dot accessor is the best way to solve your problem.

KatokichiSoft
  • 932
  • 5
  • 8
  • 1
    If the method didn't exist, the app would crash at runtime, assuming you disabled or ignored the compiler warnings... – benzado Apr 21 '11 at 00:04