App goes straight to idle on setIdleTimerDisabled: NO - ios

I'm using
[[UIApplication sharedApplication] setIdleTimerDisabled: YES];
to keep the app from entering idle, however when I issue
[[UIApplication sharedApplication] setIdleTimerDisabled: NO];
the app goes straight to idle.
Is there a way to restart the timer at this point?

No there is no way to restart the timer. Perhaps you should consider if your app really needs to do this.
Quote from Apple:
"The only applications that should disable the idle timer are mapping
applications, games, or similar programs with sporadic user
interaction."
If you app does need to turn the idleTimer off then perhaps it can stay off till the app goes in to the background?
You could try adding an alert at the end of the timed event
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:#"Timer Complete" message:#"Timer ended, well don you" delegate:nil cancelButtonTitle:#"OKAY" otherButtonTitles:nil];
[myAlert show];
[myAlert release];
Omit the release if you're using ARC. This may light the screen backup, then perhaps you don't need to mess with the idle timer.

This is obviously too late for the person who asked this question, but for anyone else who is looking, what has worked for me is to do this:
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
Including all three lines one after the other somehow resets the timer. So the timer won't dim upon immediately receiving setIdleTimerDisabled:NO but rather will take a while before it dims.

Related

Disable remote controls iOS

By default, remote control is activated on my streaming application but under a certain circumstance I want to disable all controllers and show nothing when the screen is locked.
First I tried to set now playing info to nil like this [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo: nil]; and all text, image and progress disappear but the control buttons and volume bar was still showing.
After that I tried this [[UIApplication sharedApplication] endReceivingRemoteControlEvents]; but it gives me nothing, it shows the same thing on the screen as the previous method call.
For the record, I'm calling [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; on AppDelegate before I tried to call any of the previous methods.
So, I have not found anything related to this that could help me with my problem and I would appreciate any help :)

App restarts if re-opened from app switcher

I have an app that is being restarted after being in the background for 10 minutes and then re-opened from the app switcher. I have a page (3rd page) that contains a textview, upon editing it. I pressed the home button and locked for 12 minutes, when I went back to the app via app switcher, the app restarts (goes back to the 1st page or is relaunched).
Is there a way to prevent this?
EDIT:
Turns out the my code on the applicationDidEnterBackground:
_bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:_bgTask];
}];
is causing the termination of my app in the background. any advice please?

iPhone UIAlertview abandon memory

I use UIAlertview to input some text, and it works fine.
However, after the alertview is closed, there is still more than 17MB increase in memory before alertview is opened(from 9MB to 26 MB). I used Instrument Allocation to measure heap memory and activity monitor to measure it. Both of them show the similar result. When I switch to other App, the memory increased by alertview will be reduced to the proper level. Is it normal ? My environment is iPhone4+iOS6.
Here's some code for trying.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"input caption" message:#"text"
delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
Thank you.
Dear Naveed S and Purr,
Thank you very much.
I'm not sure I follow your suggestion correctly. But I use Activity monitor watch the Real mem, the memory still increase about 20 MB after alertview is closed. I remove [alert release] after [alert show], and I wrote
[alert show];
//[alert release];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{ [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
[alertView autorelease]; }
Moreovre, the memory is reduced when the app enter background mode after I press home. (This is similar as my previous code). How could I watch the abandon memory correctly. I'm even not sure how to watch it in the right way.....
BTW, my original codes follow the post here. Uialertview and memory management
For one thing, you should autorelease your alertView. -alertView:didDismissWithButtonIndex: is called by the UIAlertView, so the alertView object should still be valid (not -release'd) when your method returns.
Better yet, convert to ARC and be done with manual -retain, -release and -autorelease ;)
If you're not converting to ARC, do this: remove the -release from the code in your original post, uncomment the //CRASH in your -alertView:didDismissWithButtonIndex: method and change the -release to -autorelease there.

iPhone display screen lights not turning off automatically

I have observed that my I run my iPhone application (native) the display lights are not turning off as they usually do in other applications, I have waited for couple of minutes but it wont turn off until I exit my application.
Is there anything which I had messed up with that is not causing the automatic dimming?
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
Are the two lines to enable/disable the dimming of the screen in an iOS app. Double check and see if your app has any of these.
Put this code into application didFinishLaunchingWithOptions: method.
Have you used this in your app?
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
If so, remove it or set it to NO.

Preventing Screenshot/clearing cached data when iOS app gets backgrounded and resumed after a long time

I am building a cinema listing app, where the user can drill down thru the dataset to finally end up with a listing for a specific movie/theater/etc.
Now assume the user pauses using the app for 7 days. When reopening the app what he should not see are the listings from 7 days ago. But if the user just puts the app in background for a few minutes, the user should continue just where he left. I thought I could solve this issue by killing the app after a certain amount of time in background. This is the code:
static BOOL goingToQuit = NO;
#define KILL_IN_BACKGROUND_AFTER_SECS 300
- (void)applicationDidEnterBackground:(UIApplication *)application
{
goingToQuit = YES;
UIApplication* app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier __block bgTask;
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
if(UIBackgroundTaskInvalid != bgTask) {
// Start the long-running task to kill app after some secs and return immediately.
dispatch_after( dispatch_time(DISPATCH_TIME_NOW, KILL_IN_BACKGROUND_AFTER_SECS * 1e09),
dispatch_get_main_queue(), ^{
if(goingToQuit) exit(0);
[app endBackgroundTask: bgTask];
});
}
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// cancel ongoing background suicide.
goingToQuit = NO;
}
What I see on my device is this: after KILL_IN_BACKGROUND_AFTER_SECS the app gets killed. After restarting the device logs show that the app got a new PID, entries showing the restart etc. Yet the device does not show the default.png startup image, but the screenshot of where the user has been before.
On the other hand if the user kills the application explicitely (double click on home button, tap & hold, click - on app) before he is restarting it the application starts with its default.png start up screen. This is the behaviour I want when killing the app programmatically.
Does anyone have an idea how to accomplish this? Any idea is highly appreciated.
BTW: As a workaround I tried to hide the main window during applicationDidEnterBackground and show it again on applicationWillEnterForeground. This, however, is highly confusing to the user when he is switching between apps.
In your app delegate's applicationDidEnterBackground you can display a view in front of the rest of your views, which the OS will grab as the last visible thing and which will be displayed when the app becomes active again (if it's still alive).
In your app delegate's applicationDidBecomeActive you can check to see if the data needs updating; if not then simply dismiss the view (animation is nice), and if so then first update (or just clear out) your data and then dismiss the view.
This is fairly common in apps. Many just use the default.png startup image, since users are accustomed to seeing it when the app launches normally.

Resources