I want to add splash screen on my app when the app is resumed from the background.Is this possible? Thanks in advance
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:]
.
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".