iOS应用程序启动图像后杀死

时间:2014-01-04 23:37:36

标签: iphone objective-c ios7 xcode5

当我杀死我的iPhone应用程序并重新启动它时,我会看到应用程序之前状态的图像,而不是启动图像。如何强制iOS始终显示启动图像?

5 个答案:

答案 0 :(得分:7)

要强制iOS启动具有默认图像的应用,请调用[[UIApplication sharedApplication] ignoreSnapshotOnNextApplicationLaunch];执行状态保存的位置。

来自documentation

  

当你的应用是   重新启动后,系统会显示此快照图像以代替您的   应用程序的默认启动图像,以保留您的应用程序的概念   仍在运行。如果您觉得快照无法正确反映   当您的应用重新启动时,您的应用的用户界面,您可以拨打电话   此方法可防止拍摄快照图像。如果你   do,UIKit使用你的应用程序的默认启动图像。

另请参阅this question了解更多详情。

答案 1 :(得分:5)

如果在状态恢复期间未调用应用程序,则不会调用

ignoreSnapshotOnNextApplicationLaunch 方法。

如果您的应用不支持多任务处理功能,其Info.plist中的UIApplicationExitsOnSuspend键设置为YES。建议的解决方案不起作用。

对我来说,我使用了以下代码:

applicationWillResignActive 用于应用程序运行时的情况,双击主页按钮调用多任务托盘。启动画面将显示在应用程序屏幕上。它完美地运作。

applicationWillTerminate 每次都无法正常工作,因为在某些阶段永远不会调用该函数

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

}

答案 2 :(得分:1)

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.

答案 3 :(得分:0)

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

}

答案 4 :(得分:-1)

对于我目前的项目,我想在应用程序被杀后强制重新启动。我将此添加到应用程序的plist中:

Application does not run in background

为确保启动画面正确,我使用了以上建议:

- (void)applicationWillResignActive:(UIApplication *)application {
[[UIApplication sharedApplication] ignoreSnapshotOnNextApplicationLaunch]; }

- (void)applicationDidEnterBackground:(UIApplication *)application {
[[UIApplication sharedApplication] ignoreSnapshotOnNextApplicationLaunch]; }

运作良好。