当用户将应用程序移至背景 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从背景中被解雇?

最佳答案

创建一个名为OverLayViewController的新“UIViewController”,并将其加载到applicationDidEnterBackground 方法

- (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代码将不适用于您的网络视图。

关于ios - 如果我将我的应用程序移至后台,然后在iOS中移至前台,则呈现的 View Controller 将被关闭,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63935739/

10-08 21:56