当应用程序/场景在iOS 13中进入前台时设置ViewController

时间:2019-09-20 17:51:54

标签: swift ios13

我的应用程序可以像通常的银行业务应用程序一样工作:当用户将应用程序移至后台(例如通过点击“主页”按钮)时,当前可见的ViewController会被启动屏幕替换。当应用回到前台时,它会显示前一个ViewController或其他ViewController(取决于许多条件,例如应用在后台的持续时间,等等-但这些在这里并不重要)。

在iOS 12及更低版本上,我使用了一种非常简单的机制-在applicationDidEnterBackground上,我将当前ViewController设置为AppDelegate上的局部变量,并显示启动屏幕。

savedViewController = window?.rootViewController

applicationWillEnterForeground上,我检查是否满足所有必需条件,并将Window的{​​{1}}设置为存储的ViewController

ViewController

这在iOS 13上不再起作用。当应用程序进入前台时,将显示初始故事野猪。在当前window?.rootViewController = savedViewController 上设置rootViewController完全没有影响。

我试图通过在应用程序中启用场景来解决此问题,但这也不起作用。

Window中,我可以在SceneDelegate方法中设置默认的ViewController,如下所示:

scene:willConnecTo

上面的代码有效,但是仅在场景首次加载时才调用该方法。然后,我尝试将代码从guard let windowScene = (scene as? UIWindowScene) else { return } window = UIWindow(frame: windowScene.coordinateSpace.bounds) window?.windowScene = windowScene let launchStoryboard = UIStoryboard(name: "LaunchScreenVersion", bundle: Bundle.main) let launchViewController = launchStoryboard.instantiateInitialViewController() window?.rootViewController = launchViewController window?.becomeKey() 移至applicationWillEnterForeground,但这也不起作用。当我通过这种方法设置sceneWillEnterForeground的{​​{1}}时,没有任何效果,只有最初加载的Window会加载。

我还尝试将代码移至ViewController,但效果完全相同。

我知道iOS具有内置的状态恢复功能,但是由于安全策略,我们无法在应用程序中使用它们,因为如果应用程序不再运行,则无法保存用户状态。

有什么想法可以让我在应用程序进入后台时将ViewController与初始屏幕交换,并在应用程序进入前景时将其重新设置?

0 个答案:

没有答案