如果我将我的应用程序移至后台,然后在iOS中移至前台,则呈现的视图控制器将被关闭

问题描述

我有一个要求,当用户将应用程序移至背景 applicationDidEnterBackground 时,必须用黑色布局覆盖屏幕,以保护屏幕上某些敏感数据的私密性。因此,为此,我利用AppDelegate函数在背景中呈现黑色,然后通过在前景 applicationDidEnterBackground 时将其消除而将其删除代码是:

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    UIViewController *blankViewController = [UIViewController new];
    blankViewController.view.backgroundColor = [UIColor blackColor];
    [self.window makeKeyAndVisible];
    [self.window.rootViewController presentViewController:blankViewController animated:NO completion:NULL];
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    [self.window.rootViewController dismissViewControllerAnimated:false completion:nil];
}

@end

现在,在我的应用程序中一切正常,但是在一个屏幕上,我通过单击以下按钮来显示 ViewContollerB [self presentViewController:webview animated:YES completion:nil];该应用程序通常在移动时会被黑色覆盖到后台,但是当我在此之后将应用程序带到前台时,显示 ViewContollerB 也被关闭了。如何防止我提出的ViewController一旦退出背景就被解雇了?

解决方法

您正在尝试执行以下操作:rootviewcontroller A呈现一个空白的viewcontrollerB。您的应用程序进入后台,并且从此处显示ViewcontrollerC。现在B关闭,因为A同时显示B和C,这是不允许的。

您将需要检查rootviewcontroller是否呈现任何视图控制器,以及是否递归呈现其他视图控制器。您可以使用视图控制器上的属性presentedViewController来检查这些内容。

就个人而言,我会(在所有视图控制器都继承的基类的视图控制器中)保留一个变量,以检查该变量是否可见(通过跟踪viewDidAppearviewDidDisappear)。如果这是可见的,则在顶部添加一个空白视图。

在Swift中回答,因为没有编辑器就无法信任Objective-C:

class BaseViewController: UIViewController {
    var appeared = false

    func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated: animated)
        appeared = true
    }

    func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated: animated)
        appeared = false
    }
}

然后,您需要从AppDelegate中触发该视图控制器中捕获的通知,然后在顶部显示空白视图。

,

创建一个名为applicationDidEnterBackground 的新'UIViewController'并将其加载到 - (void)applicationDidEnterBackground:(UIApplication *)application { OverLayViewController *blankViewController = [OverLayViewController new]; blankViewController.view.backgroundColor = [UIColor blackColor]; [self.window makeKeyAndVisible]; UIViewController *rvc = self.window.rootViewController; UIViewController *pvc = rvc.presentedViewController; // you may need to loop through presentedViewControllers if you have more than one if(pvc != nil) { [pvc presentViewController: blankViewController animated: NO completion:nil]; } else{ [self.window.rootViewController presentViewController: blankViewController animated: NO completion:nil]; } } - (void)applicationWillEnterForeground:(UIApplication *)application { UIViewController *test = [self topViewController]; if ([test isKindOfClass:[OverLayViewController class]]) { [test dismissViewControllerAnimated:false completion:nil]; } } - (UIViewController*)topViewController { return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; } -(UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController { if ([rootViewController isKindOfClass:[UITabBarController class]]) { UITabBarController* tabBarController = (UITabBarController*)rootViewController; return [self topViewControllerWithRootViewController:tabBarController.selectedViewController]; } else if ([rootViewController isKindOfClass:[UINavigationController class]]) { UINavigationController* navigationController = (UINavigationController*)rootViewController; return [self topViewControllerWithRootViewController:navigationController.visibleViewController]; } else if (rootViewController.presentedViewController) { UIViewController* presentedViewController = rootViewController.presentedViewController; return [self topViewControllerWithRootViewController:presentedViewController]; } else { return rootViewController; } } 方法

dismissViewContoller

在这种情况下,@deprecated代码将不适用于您的网络视图。