iOS App Launch Image After a Kill

耗尽温柔 提交于 2019-12-04 12:51:26

问题


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?


回答1:


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.




回答2:


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

}



回答3:


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.



回答4:


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()
        }

}



回答5:


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.



来源:https://stackoverflow.com/questions/20928106/ios-app-launch-image-after-a-kill

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!