Pantelis Proios
Pantelis Proios

Reputation: 1369

iPhone : User option to keep the app awake

I have a count down timer that "beeps" every second it counts through AVAudioPlayer. i want to set a user option to keep the app awake through an on/off switch. i know that in order to keep the app awake you need to set [UIApplication sharedApplication].idleTimerDisabled = YES; however how would i control the YES or NO option through a switch? Also, i noticed that after keeping the app awake for more than 5 minutes the audio (a beep sound in my case) is muted? Why is that and how can i avoid it?

Upvotes: 3

Views: 2591

Answers (1)

Philipp Schlösser
Philipp Schlösser

Reputation: 5219

You need a UISwitch. Just connect it to the following method to your .m:

- (void) switchChanged:(id)sender {
    UISwitch* switch = sender;
    [UIApplication sharedApplication].idleTimerDisabled = switch.on;
}

Of course, you need to set the correct value initially:

mySwitch.on = [UIApplication sharedApplication].idleTimerDisabled;

And just for completion, you have to add the following lines to your .h and connect them in you .nib file:

@interface YourClass{
    ...
    IBOutlet UISwitch* mySwitch;
}

- (void) switchChanged:(id)sender;

Upvotes: 4

Related Questions