ios – Tips on how to dismiss view controller from SceneDelegate?

[ad_1]

I’m engaged on an ios App (Swift 5) and I’m making an attempt to point out a display screen if the app is offline after which dismisses when the consumer re-connects.

I’m anticipating the OfflineViewController to seem when the consumer is offline, and the final display screen the consumer on to seem if they’re linked.

What is occurring is the OfflineViewController is showing once I disconnect from the community, nonetheless it doesn’t go away once I join again to the community. I attempted including a button to dismiss and this doesn’t work both.

I’ve connected my code under, any thought what am I doing incorrect?

SceneDelegate

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?
    let reachability = attempt! Reachability()


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, choices connectionOptions: UIScene.ConnectionOptions) {
        
        guard let _ = (scene as? UIWindowScene) else { return }

        // Ship to homepage if logged in, in any other case login display screen.
        let accessToken: String? = KeychainWrapper.customary.string(forKey: "accessToken")
        
        // If entry token exists, skip login web page
        if accessToken != nil {            
            if let windowScene = scene as? UIWindowScene {
                self.window = UIWindow(windowScene: windowScene)
                let mainStoryboard:UIStoryboard = UIStoryboard(identify: "Foremost", bundle: nil)
                let vc = mainStoryboard.instantiateViewController(withIdentifier: "homeTabController") as! TabBarController
                self.window!.rootViewController = vc
            }
        }

        reachability.whenUnreachable = { [self] _ in
            print("Not reachable (Scene delegate)")

            let storyboard = UIStoryboard(identify: "Foremost", bundle: nil)
            
            let vc = storyboard.instantiateViewController(withIdentifier: "OfflineViewController") as! OfflineViewController
            vc.modalPresentationStyle = .fullScreen

            guard let firstScene = UIApplication.shared.connectedScenes.first as? UIWindowScene else {
                return
            }

            guard let firstWindow = firstScene.home windows.first else {
                return
            }
            
            let rootVC = firstWindow.rootViewController
            rootVC?.dismiss(animated: true, completion: nil)
            rootVC!.current(vc, animated: true, completion: nil)

        }

        do {
            attempt reachability.startNotifier()
        } catch {
            print("Unable to start out notifier")
        }
    }
}

OfflineViewController

import UIKit

class OfflineViewController: UIViewController {
    
    let reachability = attempt! Reachability()

    override func viewDidLoad() {
        tremendous.viewDidLoad()

        do {
            attempt reachability.startNotifier()
        } catch {
            print("Unable to start out notifier")
        }
    }
    
    @IBAction func hitRefresh(_ sender: Any) {
        reachability.whenReachable = { reachability in
                self.dismiss(animated: true, completion: nil)
        }
    }
}

[ad_2]

Leave a Reply