I don't want my app to go to sleep (shut screen off) unless the user puts it out of its misery. I'm trying what I think is simple code:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
application.idleTimerDisabled = YES;
This works most of the time, but the app still occasionally goes to sleep. I'm not sure of the pattern. Is there anything that can reenable the idle timer?
Some classes (such as MPMoviePlayerController) will enable/disable the idleTimer as part of their normal workings.
rpetrich is right.
I was launching a MoviePlayerController out of my app and on the callback I was forced to set idleTimerDisabled to YES again... But there's a remark here because for some reason iphone is discarding this set...
I solved it, this way:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
application.idleTimerDisabled = NO;
application.idleTimerDisabled = YES;
}
Related
When i keep my application open for awhile, the iPhone/iPod locks the screen. When i unlock it my application gets stuck for like 2 seconds and then it resumes and keep functioning as usual. Why is this ? and how can i prevent it ?
To prevent this from hapenning is there any PLIST method where we could stop the process of the application when it goes to a locked screen (Might not be a better idea)
In your application delegate do you have any code that could slow down your app? Check the following methods?
-(void) applicationWillResignActive:(UIApplication *)application
-(void) applicationDidBecomeActive:(UIApplication *)application
-(void) applicationDidEnterBackground:(UIApplication*)application
-(void) applicationWillEnterForeground:(UIApplication*)application
-(void) applicationWillTerminate:(UIApplication *)application
Also use the above methods ensure you application suspends properly.
Log when your app receives a memory warning inside:
-(void) applicationDidReceiveMemoryWarning:(UIApplication *)application
Maybe when you suspend or reopen your app some there is a memory issue.
I'm not quite sure about the answer for your first question (you maybe do some heavy things inside the applicationDidBecomeActive method or the app simply reallocates memory), but i can answer the second one.
You can simply prevent the auto lock by calling:
[[UIApplication sharedApplication] setIdleTimerDisabled: YES];
A good palce for this is inside the applicationDidFinishLaunching method of the app delegate.
I'm developing an music/video player app and just need to konw how to disable the auto-lock while my app is in foreground.
I know I've to use [[UIApplication sharedApplication] setIdleTimerDisabled:YES]; and [[UIApplication sharedApplication] setIdleTimerDisabled:NO]; at some point, but where is the best place to put them?
Enable the idle timer in
- (void)applicationWillResignActive:(UIApplication *)application
and disable it in
- (void)applicationDidBecomeActive:(UIApplication *)application
The best place to disable it is in didFinishLaunchingWithOptions. The system will automatically take care of making the setting have no effect when the app is in the background.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
application.idleTimerDisabled = YES;
return YES;
}
I posted this alternative because the accepted answer does not prevent auto-lock when an alert appears (email, message, calendar event etc), or the notification center or control center is up.
Swift 3.0:
Inside AppDelegate.swift:
application.idleTimerDisabled = true
Outside AppDelegate.swift:
UIApplication.shared().isIdleTimerDisabled = true
And in Swift 3.0:
UIApplication.shared().isIdleTimerDisabled = true
my 2 cents: for xcode 9:
application.idleTimerDisabled = true
.....AppDelegate.swift:28:15: 'idleTimerDisabled' has been renamed to 'isIdleTimerDisabled'
so:
application.isIdleTimerDisabled = true
Is it possible for my app to "take over" when a system alert pops up? My app disables the idle timer, but when a system alert pops up, the alert seems be enabling the timer. What can I do about that?
Would it be possible for you to hook into one of the methods that gets called when your app goes in and out of the background? Say, one of these?
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(#"applicationWillResignActive");
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(#"applicationDidEnterBackground");
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(#"applicationWillEnterForeground");
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"applicationDidBecomeActive");
}
I bet one of those gets called when that happens. You can probably disable the idle timer when your app gets the focus back.
EDIT: On re-reading the question, it looks like you're looking to suppress the alerts (i.e., not let them happen). Heh, I focused on the latter half of your question.
Are you trying to suppress the alerts? Then the answer is: you can't.
If you're trying to prevent the alerts from messing with your app's disabling of the idle timer, then I believe donkim is on the right trail.
I have an application that requires the iPhone screen to remain active (or not, depending on user choice). I've done this by disabling the application idle timer, which works fine and dandy until I start playing media via the MPMusicPlayerController. Due to a bug in the SDK, this then reenables the idle timer with no apparent way to disable it again.
My app flow is:
App starts
Screen stays on
<...time passes...>
Play audio file
Idle timer kicks in
Screen turns off
I have an empty audio file playing in the background to stop the phone going into deep sleep, but I'd really like to keep the screen unlocked too.
Has anyone managed to figure out a workaround for this?
I had a similiar problem, and found a fix for it. The fix might work for you too:
I call a method periodically (every 10 seconds), which sets idleTimerDisabled first to NO, then to YES.
- (void)calledEveryTenSeconds
{
[UIApplication sharedApplication].idleTimerDisabled = NO;
[UIApplication sharedApplication].idleTimerDisabled = YES;
}
Only setting to YES alone does not fix the problem. It seems the property has to change first to be recognized by UIApplication.
My problem was, that the screen kept turning dark as soon as I switched music tracks on the iPod player via the headphone remote. My guess is, that this is the same issue as you are experiencing.
You should simply turn off the idle timer. What I usually do in a viewcontroller that needs to stay 'awake' is this:
- (void) viewWillAppear:(BOOL)animated
{
[[UIApplication sharedApplication] setIdleTimerDisabled: YES];
}
- (void) viewWillDisappear: (BOOL) animated
{
[[UIApplication sharedApplication] setIdleTimerDisabled: NO];
}
This will make sure the screen will not get locked due to user inactivity.
I found a solution to this problem. Invoke a method that disables the idleTimer in about 5 seconds after you start playing the music. It's a bit of a hack, but it is a workaround.
[[SoundEngine mainEngine] playMusic];
[self performSelector:#selector(setIdleTimeDisabled) withObject:nil afterDelay:5.0];
- (void) setIdleTimeDisabled {
[UIApplication sharedApplication].idleTimerDisabled = YES;
NSLog(#"Setting idleTimer to TRUE");}
let player = MPMusicPlayerController.applicationMusicPlayer()
player.setQueueWithStoreIDs(["some id"])
player.play()
player.pause()
Is there any way to detect if the iPhone wakes up from sleep while you're app is running? Eg: your app is running, the user locks the screen (or the screen auto locks) and some time later the user unlocks the screen and up pops your app. Is there some way to get an event at that point or detect it somehow?
I've tried searching the Google and this forum, but I can't seem to find anything about it.
See applicationDidBecomeActive: on UIApplicationDelegate.
Stick these in you AppDelegate.m file:
-(void) applicationWillResignActive:(UIApplication *)application {
NSLog(#"Asleep");
}
-(void) applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"Awake");
}
#Kevin - Nothing wrong with your answer - thanks by the way. Just thought I'd save the next person a Google search.