1

I want to add splash screen on my app when the app is resumed from the background.Is this possible? Thanks in advance

nyanev
  • 11,299
  • 6
  • 47
  • 76

2 Answers2

6

You can update your view stack in -[UIApplicationDelegate applicationWillResignActive:].

The changes will be visible when the app resumes, and you can remove the splash screen again in -[UIApplicationDelegate applicationDidBecomeActive:].

Morten Fast
  • 6,322
  • 27
  • 36
3

Some code to go along with Morten's answer. I also want to note that this does not behave properly in the simulator, but does when you run it on the device. The simulator shows a black screen until removeFromSuperview is called.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // We don't want to show a splash screen if the application is in UIApplicationStateInactive (lock/power button press)
    if (application.applicationState == UIApplicationStateBackground) {
        UIImageView *splash = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"splashimage.png"]];
        splash.frame = self.window.bounds;
        [self.window addSubview:splash];
    }

}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Make sure you do not remove the last view in the event something odd happens
    if ([[self.window subviews] count] > 1) {
        // Not recommended by Apple, but client gets what client wants
        [NSThread sleepForTimeInterval:1.0];
        [[[self.window subviews] lastObject] removeFromSuperview];
    }
}

Note, I used applicationDidEnterBackground instead of applicationWillResignActive because applicationState helps to differentiate between "pressed home button" and "pressed lock/power button". UIApplicationStateBackground = "pressed home button".

Jon
  • 3,208
  • 1
  • 19
  • 30
  • 1
    `applicationDidEnterBackground:` is the right place to do this, according to [Apple's iOS App Programming Guide](http://developer.apple.com/library/ios/DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW35) (see "What to Do When Moving to the Background.... Prepare to have their picture taken"). – bunnyhero Feb 02 '13 at 09:40