gpt4 book ai didi

ios - 如果我将我的应用程序移到后台然后在 iOS 中进入前台,则呈现的 View Controller 会被解散

转载 作者:行者123 更新时间:2023-12-01 19:29:27 27 4
gpt4 key购买 nike

当用户将应用程序移至后台时,我需要用黑色布局覆盖屏幕 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代码不适用于您的 webview。

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com