Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS black screen when bringing app to foreground after some time in background

I'm trying to remove all storyboards from our iOS app as they are a huge mess when working in a team with Git.

I'm now setting the initial ViewController in AppDelegate's application(_:didFinishLaunchingWithOptions:) method:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow(frame: UIScreen.main.bounds)
    let launchViewController = LaunchView()
    window!.rootViewController = launchViewController
    window!.makeKeyAndVisible()

    [...]

    return true
}

LaunchView is a simple view controller responsible of routing the user to login or main screen depending if he/she is logged in.

Before this, LaunchView was set as initial in Main.storyboard.

I already removed these lines from Info.plist file:

<key>UIMainStoryboardFile</key>
<string>Main</string>

Everything is working fine, except when we leave the app in background for a couple hours without force-quitting it (I'm not sure how much time is needed to reproduce this) and then bring the app back to foreground, an all-black screen is shown, as if the root controller disappeared. And we have to kill the app and reopen it again in order to use it.

This is driving me crazy because it is really hard to reproduce, since if you leave the app in background only for a few minutes it works fine. I'm suspecting it has something to do with the app suspended state, but I'm not really sure.

Do you know where the problem might be?

Thank you!

EDIT

Maybe this has something to do with the problem: On the LaunchView, I'm sending the user to the main screen (if he is logged in) with the following code:

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }

let rootVC = UIStoryboard.main.rootViewController
if let snapshot = appDelegate.window?.snapshotView(afterScreenUpdates: true) {

    rootVC.view.addSubview(snapshot);
    appDelegate.window?.rootViewController = rootVC

    UIView.transition(with: snapshot, duration: 0.4, options: .transitionCrossDissolve, animations: {
        snapshot.layer.opacity = 0;
    }, completion: { (status) in
        snapshot.removeFromSuperview()
    })
}
else {
    appDelegate.window?.rootViewController = rootVC
}

Is it possible that iOS is removing rootViewController at some point?

like image 229
Rouger Avatar asked Feb 28 '19 16:02

Rouger


1 Answers

Turns out the problem was related to the transition effect involving a snapshot when swaping the window's rootViewController.

I removed the transition effect and simply changed the rootViewController. Now it's working fine!

In code, I changed this:

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }

let rootVC = UIStoryboard.main.rootViewController
if let snapshot = appDelegate.window?.snapshotView(afterScreenUpdates: true) {

    rootVC.view.addSubview(snapshot);
    appDelegate.window?.rootViewController = rootVC

    UIView.transition(with: snapshot, duration: 0.4, options: .transitionCrossDissolve, animations: {
        snapshot.layer.opacity = 0;
    }, completion: { (status) in
        snapshot.removeFromSuperview()
    })
}
else {
    appDelegate.window?.rootViewController = rootVC
}

To this:

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let rootVC = UIStoryboard.main.rootViewController
appDelegate.window?.rootViewController = rootVC

Thank you all for your help! I hope this answer is useful to somebody else.

like image 93
Rouger Avatar answered Nov 05 '22 18:11

Rouger