当我杀掉我的iPhone应用程序并重新启动它时,我看到的是应用程序先前状态的图像,而不是启动图像。我该如何强制iOS始终显示启动图像?
ignoreSnapshotOnNextApplicationLaunch 方法将不会在应用程序未在状态恢复期间调用时被调用。
如果您的应用程序不支持多任务功能,即 UIApplicationExitsOnSuspend 键在其 Info.plist 中设置为 YES。建议的解决方案将无法工作。
对于我来说,我使用了以下代码:
applicationWillResignActive 用于当应用程序运行时,您双击 home 按钮调用多任务托盘的情况。启动画面将显示在应用程序屏幕上。它运行得非常完美。
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];
}
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.
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()
}
}
针对我的当前项目,我想在应用程序关闭后强制重新启动。我将以下代码添加到应用程序的plist中:
- (void)applicationWillResignActive:(UIApplication *)application {
[[UIApplication sharedApplication] ignoreSnapshotOnNextApplicationLaunch]; }
并且
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[UIApplication sharedApplication] ignoreSnapshotOnNextApplicationLaunch]; }
它运行良好。