Iphone application gets stuck after unlocking screen - iphone

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.

Related

How to know if a task is executing in background

In my app I'm downloading lots of images on a method.
I'm using a
downloadTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:downloadTask];
downloadTask = UIBackgroundTaskInvalid;
}];
This is working fine, if I press the home or sleep button, the images continue downloading.
I'm showing the progress in a UIProgressView inside an UIAlertView, and when the percent is 100% the alertView is dissmised and I change the viewController to other where I show the donwloaded images.
But I only want this to happen if the app is really active at the moment the download finish.
I have been looking at the app state and while it's downloading with the screen off.
[UIApplication sharedApplication].applicationState
the state is UIApplicationStateActive during all the donwload
How can I can know if the downloading is happening with the screen off or on?
EDITED AFTER ACCEPTING THE ANSWER:
I just discovered, if I tap the home button, the app enters in UIApplicationStateBackground, if I tap the wake/sleep it enters in UIApplicationStateInactive
Following the approach of the correct answer, my app contines donwloading in both cases.
The screen is off in two states (apart from when the app has not been even opened):
suspended : in this case you don't have to worry because the download won't procede until the app gets active again; It will enter this state on
background : it's in this state for a limited amount of time before going in suspend, and the screen is already off in this moment. Here you may want to check then whether to do all the things you said or not, because in this state code can be still executed. In this state the app status is UIApplicationStateBackground, so you could just perform a check like this:
You probably want to check whether the app is in background execution in order to achieve the result. Just like this:
if([[UIApplication sharedApplication] applicationState] != UIApplicationStateBackground) {
// Do stuff
}
If it's in background, so the screen is off.
UPDATE: after few test, what I figured out is that the behaviour you are expieriencing is probably due to the execution of the download on the main thread.
You should send the download on (for instance) the global queue. This way the application will enter the background state as expected:
....
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.bti];
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self doBackgroundStuff];
});
....
This way, if the app is put on background while the download is in progress, the application state will turn into UIApplicationStateBackground, and then you can check it as I wrote initially. If you are doing UI updates during the progress remember to send them back to the main thread (because the download is now on a different one).
You can check whether your app is running in the background or not by setting a flag in the designated application delegate methodsapplicationDidEnterBackground: and applicationWillEnterForeground:. Example:
- (void)applicationDidEnterBackground:(UIApplication *)application
_applicationRunsInForeground = NO;
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
_applicationRunsInForeground = YES;
}
If you don't want to have this _applicationRunsInForeground flag inside your application delegate, you could observe the delegate's NSNotifications in your viewcontroller class instead (UIApplicationWillEnterForegroundNotification and UIApplicationDidEnterBackgroundNotification).

Screen Suddenly Turns Black

While using my app, the screen sometimes suddenly and inexplicably turns black. The screen is still powered, and if I play with the volume controls, the volume indicator shows just fine.
The only way to resolve this is to press the home button, at which point I see the following message in my log:
SpringBoard[15] : Failed to snapshot WaveDeck
When I then open the app again, it's fine.
Can anyone shed some light on this?
Update from the WaveDeck front:
We've used applicationDidEnterBackground in the app's delegate in a bad way: asking all the background operations to finish up using beginBackgroundTaskWithExpirationHandler and keeping the main runloop running inside applicationDidEnterBackground.
That way the method of applicationDidEnterBackground came to an end only after finishing all background operations, whether the app became active again or not.
So apparently the OS could not 'snapshot' the app correctly and would put on a black screen some time after returning from background state - when the delegate function of applicationDidEnterBackground ended.
I encounter a similar issue. Put the long time save operation in next runloop, so applicationDidEnterBackground return immediately, the system's snapshot mechanism works correctly.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// don't block ios's snapshoting, avoid screen suddenly turns black
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(saveAppState) object:nil];
[self performSelector:#selector(saveAppState) withObject:nil afterDelay:0];
}
- (void)saveAppState
{
[self longtimeSave];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(saveAppState) object:nil];
}

- (void)applicationWillResignActive:(UIApplication *)application never gets called

I'm trying to handle phone calls and standby and things of that nature. I added the function
- (void)applicationWillResignActive:(UIApplication *)application
and
- (void)applicationDidBecomeActive:(UIApplication *)application
to my UIApplicationDelegate. When coming out of standby, "applicationDidBecomeActive" always gets called. However the issue is "applicationWillResignActive" never gets called.
I was wondering if anyone has run into this issue and whether or not anyone found a reason.
EDIT
More info, I discovered that my engine's update loop that gets run from applicationDidFinishLaunching was causing me to miss the message. I call
while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, .002, FALSE) == kCFRunLoopRunHandledSource);
to catch all iphone messages but it doesn't seem to catch the resignActive message before becoming inactive.
Attempting to fork a thread for my update loop is causing weird crash bugs. Anyone have any quick fix suggestions?
Its getting called in iOS 4.0 , when the Home button is hit.
The following delegate methods are called when the Home button is hit in iOS 4.0
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(#"Application Did Resign Active");
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"Application Did Enter Background");
}
And when you double tap the home button and again relaunch the App , the following delegate methods are called .
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(#"Application Will Enter Foreground");
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(#"Application Did Become Active");
}
I don't think
- (void)applicationWillResignActive:(UIApplication *)application
is called when a phone call is received. I think the OS waits for the user to either answer or declines the phone call. If it i declined, then the app says alive and
- (void)applicationDidBecomeActive:(UIApplication *)application
is called. If it is answered, then your app is told to exit and it will receive
- (void)applicationWillTerminate:(UIApplication *)application
Be sure to allow
- (void)applicationDidFinishLaunching:(UIApplication *)application
to return before running your game loop. One technique is to use the function
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
on the application delegate and run your game loop after a delay of say ".01" After you do this, the message should be able to fire properly. I believe the reason for the message stomping was because the run loop was stuck on applicationDidFinishLaunching and wasn't able to push any other UIApplicationDelegate messages onto the queue.
- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active state to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}

How to reliably avoid iphone idle?

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;
}

Awake from sleep event on the iPhone?

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.