Friday, December 2, 2022
HomeiOS Developmentios - Easy methods to dismiss view controller from SceneDelegate?

ios – Easy methods to dismiss view controller from SceneDelegate?


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

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

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

I’ve hooked up my code beneath, 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.
        let accessToken: String? = KeychainWrapper.normal.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(title: "Major", 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(title: "Major", 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 begin notifier")
        }
    }
}

OfflineViewController

import UIKit

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

    override func viewDidLoad() {
        tremendous.viewDidLoad()

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments