Ahmed
Ahmed

Reputation: 1359

Getting the name of UIVIewController in App delegate swift

Currently I implemented the Reachability is AppDelegate.swift file. This is my code to determine when I connect or disconnect to the internet

@objc func reachabilityChanged(note: Notification) {
    let reachability = note.object as! Reachability
    if (reachability.connection != .none) {
    }
    else
    {
        currentView()
    }

}

This code works fine to detect if the internet is connected or disconnected. Now if the internet is disconnected I want to know which viewController I am currently at in my navigationController and I would like to modify the UI of that viewController to notify the user that the Internet Is disconnected. I am trying to get the current viewController in my navigationStack using the following code but it does not work

if let window = UIApplication.shared.delegate?.window {
    if var viewController = window?.rootViewController {
        // handle navigation controllers
        if(viewController is UINavigationController){
            viewController = (viewController as! UINavigationController).visibleViewController!
        }
        print (viewController)
    }
}

Upvotes: 0

Views: 808

Answers (1)

Snezhana
Snezhana

Reputation: 100

I would suggest you use NotificationCenter and send a notification to the observers to do what you want if their view is on screen. Make your VCs observers and implement selectors when notification is sent.

Check this answer if you don't know how to check if the view is on screen: How to tell if UIViewController's view is visible

Upvotes: 1

Related Questions