iPhone - phone goes to sleep even if idleTimerDisabled is YES

前端 未结 9 1357
难免孤独
难免孤独 2020-11-30 03:45

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

[appl         


        
相关标签:
9条回答
  • 2020-11-30 04:01

    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

    0 讨论(0)
  • 2020-11-30 04:04

    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 04:05

    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.

    0 讨论(0)
  • 2020-11-30 04:05

    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

    0 讨论(0)
  • 2020-11-30 04:06

    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.

    0 讨论(0)
  • 2020-11-30 04:10

    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
    }
    
    0 讨论(0)
提交回复
热议问题