When I kill my iPhone app and relaunch it I have an image of the app\'s previous state instead of the launch image. How can I force iOS to always show the launch image?
For my current project I wanted to force re-launch after the app is killed. I added this to the app's plist:
To insure the splash screen was correct, I used the suggestions above:
- (void)applicationWillResignActive:(UIApplication *)application {
[[UIApplication sharedApplication] ignoreSnapshotOnNextApplicationLaunch]; }
and
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[UIApplication sharedApplication] ignoreSnapshotOnNextApplicationLaunch]; }
It is working well.
ignoreSnapshotOnNextApplicationLaunch method will not be called if the app is not invoked during state restoration.
If your app is NOT supporting Multitasking feature, which the UIApplicationExitsOnSuspend key in its Info.plist is Set to YES. The suggested solution will not work.
For me, I used the code below:
applicationWillResignActive is used for the situation when the app is running, you double click the home button to call the multitasking tray. The splash screen will show on the application screen. It works perfectly.
applicationWillTerminate is not working every time because the function could be never called on some stages
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(@"Application Did Resign Active");
self.viewController.webView.hidden = YES;
NSString *splashImage;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
splashImage = @"Default-Portrait~ipad.png";
}
else {
splashImage = @"Default~iphone.png";
}
UIImageView *splash = [[UIImageView alloc]initWithFrame:[self.window frame]];
[splash setImage:[UIImage imageNamed:splashImage]];
[self.window addSubview:splash];
}
- (void)applicationWillTerminate:(UIApplication *)application {
NSLog(@"Application is Terminated");
self.viewController.webView.hidden = YES;
NSString *splashImage;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
splashImage = @"Default-Portrait~ipad.png";
}
else {
splashImage = @"Default~iphone.png";
}
UIImageView *splash = [[UIImageView alloc]initWithFrame:[self.window frame]];
[splash setImage:[UIImage imageNamed:splashImage]];
[self.window addSubview:splash];
}
To force iOS to launch an app with its default image, call [[UIApplication sharedApplication] ignoreSnapshotOnNextApplicationLaunch];
where you implement state preservation.
From the documentation:
When your app is relaunched, the system displays this snapshot image in place of your app’s default launch image to preserve the notion that your app was still running. If you feel that the snapshot cannot correctly reflect your app’s user interface when your app is relaunched, you can call this method to prevent that snapshot image from being taken. If you do, UIKit uses your app’s default launch image instead.
Also look at this question for more details.
Swift 4.2
func applicationDidEnterBackground(_ application: UIApplication) {
let imageView = UIImageView(frame: self.window!.bounds)
imageView.tag = 25
imageView.image = UIImage(named: <YOUR_SPLASH_SCREEN_IMG>)
UIApplication.shared.keyWindow?.subviews.last?.addSubview(imageView)
}
func applicationWillEnterForeground(_ application: UIApplication) {
if let imageView : UIImageView = UIApplication.shared.keyWindow?.subviews.last?.viewWithTag(25) as? UIImageView {
imageView.removeFromSuperview()
}
}
Please follow this -
-(void)applicationWillResignActive:(UIApplication *)application
{
imageView = [[UIImageView alloc]initWithFrame:[self.window frame]];
[imageView setImage:[UIImage imageNamed:@"Portrait(768x1024).png"]];
[self.window addSubview:imageView];
}
Here to remove your imageview:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if(imageView != nil) {
[imageView removeFromSuperview];
imageView = nil;
}
}
It is working and tested many times.