Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - phone goes to sleep even if idleTimerDisabled is YES

Tags:

iphone

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

like image 901
lostInTransit Avatar asked Nov 05 '09 11:11

lostInTransit


People also ask

Why does my iPhone keep going to sleep?

The reason your iPhone's screen keeps dimming and turning off is because of a feature called “Auto-Lock,” which automatically puts the iPhone into a sleep/lock mode after a certain period of time. Two-thirds of the way through the set period, the screen dims to half brightness.

How do I keep my iPhone on streaming?

Here's how to turn it on or off: Go to Settings > General > Auto-Lock. Choose an amount of inactivity* (from 30 Seconds up to 5 Minutes) that will turn off and lock your screen. Or to turn off Auto-Lock, select Never.

Why does my iPhone go to sleep while watching youtube?

What you are describing is the auto-lock feature. You can adjust this setting to be able to watch your videos without interruption by going to Settings > Display & Brightness > Auto-Lock.


2 Answers

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.

like image 97
Kenny Winker Avatar answered Sep 20 '22 08:09

Kenny Winker


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.

like image 27
Mike Gledhill Avatar answered Sep 21 '22 08:09

Mike Gledhill