Reputation: 13
I am trying to disable the automated sleeping of iPhone for certain period in my app. Used [[UIApplication sharedApplication] setIdleTimerDisabled:YES] which works fine as long as I play no music.
But when I play music the Idle Timer seems to get reactivated.
I have tried all kinds of tricks from NSTimer firing silent sounds every 10 second etc but nothing works.
Would welcome any suggestion or thoughts on making this happen.
Upvotes: 0
Views: 797
Reputation: 2585
The way to fix this is to first subclass UIApplication, override the setIdleTimerDisabled method and make it do nothing. Then, add a couple of your own methods that you'll call from your application instead of using the normal setter. By doing this you will ignore all messages that might change the idle timer aside from the custom method calls you make yourself. Here's how you do it:
Edit your main.m file to use a custom UIApplication subclass:
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString* appClass = @"CustomUIApplicationSubclass";
NSString* delegateClass = nil;
int retVal = UIApplicationMain(argc, argv, appClass, delegateClass);
[pool release];
return retVal;
}
Then define your UIApplication subclass:
@interface CustomUIApplicationSubclass : UIApplication {
}
- (void)disableIdleTimer;
- (void)enableIdleTimer;
@end
@implementation CustomUIApplicationSubclass
- (void)setIdleTimerDisabled:(BOOL)disabled
{
// do nothing! take that stupid ipod controller!
}
- (void)enableIdleTimer
{
[super setIdleTimerDisabled:NO];
}
- (void)disableIdleTimer
{
[super setIdleTimerDisabled:YES];
}
@end
This will force the iPod controller to use your custom UIApplication instance which does no longer does anything when the normal setIdleTimerDisabled method is called.
Upvotes: 1