0

I have an iPad app where the user can set the idleTimerDisabled to YES or NO via a switch in preferences. That part works fine. However, initially setting it to YES in the app delegate's didFinishLaunchingWithOptions method if it's the first time the app has run doesn't work (the device auto-sleeps anyway).

I've tried the hack of setting it to NO first, then to YES, as described in other threads to no avail. All other aspects of the preferences (standardUserDefaults) are working fine, as well.

Here's the relevant code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // if app run for the first time, set these as defaults
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    if (![prefs objectForKey:@"autoSleep"]) {
    // this conditional code runs, as traced using NSLog   
    [prefs setBool:YES forKey:@"autoSleep"];
    application.idleTimerDisabled = NO;
    application.idleTimerDisabled = YES;
    }
}
Gregir
  • 1,574
  • 5
  • 21
  • 43
  • Are you sure that the timer really is misbehaving. Remember that the getter method is `-isIdleTimerDisabled`. – PengOne Sep 08 '11 at 23:03
  • Good question. I hadn't checked that, but [application isIdleTimerDisabled] returns 1. The device sleeps anyway. – Gregir Sep 08 '11 at 23:15
  • Long shot, but try using the long form: `[application setIdleTimerDisabled:YES];` – PengOne Sep 08 '11 at 23:22
  • It absolutely shouldn't be necessary, but do you see different behaviour if you relegate the idleTimerDisabled to a separate method and do a quick `performSelector:withObject:afterDelay:0`? – Tommy Sep 08 '11 at 23:29
  • Nuts. Those were good ideas, but they didn't work either. I'm getting the app on the iPad via Run in XCode while targeting the device. I'm noticing that it works in that initial testing "Run." It's when I stop it via XCode then run the app by tapping the icon on the iPad that it doesn't work. It's got to have something to do with that, I'm guessing. – Gregir Sep 08 '11 at 23:44
  • This did indeed turn out to be an issue about the initial install being via XCode. When I deleted the app off the device and instead of installing via a text in XCode I installed by archiving the app and putting it in via iTunes, it worked fine. Thanks for the ideas, though. – Gregir Sep 09 '11 at 00:27

1 Answers1

1

Use the registerDefaults method of NSUserDefaults instead of testing if objectForKey is nil.

See also details about this in the relevant Programming Guide. Once you have register default values using registerDefaults (in your case the value NO for your "autoSleep" key), you are ensured that you will have a value in this key, either the one set in the application's settings by the user… or this default one if the user hasn't set a value for it yet.

Thus it should solve your problem as you will always have a value for your autoSleep key, either the default one or the user-provided one.

AliSoftware
  • 32,623
  • 6
  • 82
  • 77
  • This didn't fix the issue (it turned out to be the initial install going via XCode), but your point about registerDefaults is well-taken. Thanks for the tip. – Gregir Sep 09 '11 at 00:26
  • I continued to have weird issues where my idle timer would be disabled when it shouldn't be, and not disabled when it should be. Although I'm not sure the issue is 100% cleared up yet, I seem to have fewer problems after using registerDefaults. – Gregir Sep 15 '11 at 04:52
  • @AliSoftware, your comment implies that there's a potential issue or downside to using NSUserDefaults:objectForKey to test if a key is present. can you be more specific? – Drew O'Meara Jun 06 '15 at 19:52