Search code examples
iosswiftreachability

Facing Issue with Reachability IOS Swift


I Created Reachability in one Demo project in that project i have two view controllers if internet connection is there i am displaying OnlineViewController if internet connection is not there I am displaying OfflineViewController. See the following Code which i have implemented in Appdelegate.swift.

 var reachability:Reachability?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        NotificationCenter.default.addObserver(self, selector: #selector(reachabilityStatusChanged(_:)), name: .reachabilityChanged, object: nil)
        reachability = Reachability.forInternetConnection()
        reachability!.startNotifier()
            return true
    }

And My function is like This

 @objc func reachabilityStatusChanged(_ sender: NSNotification) {

        var remoteHostStatus = self.reachability!.currentReachabilityStatus()

        if (remoteHostStatus == NotReachable)
        {
            print ("no net")
            let testController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "OfflineViewController") as! OfflineViewController
            window!.rootViewController = testController
            window!.makeKeyAndVisible()
        }
        else
        {
            let testController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "OnlineViewController") as! OnlineViewController
            window!.rootViewController = testController
            window!.makeKeyAndVisible()
            print (" wifi")

        }

    }

Now my question is i want to implement same thing in my realtime project in that project i have 3 storyboards and mutiple viewcontrollers. so how to display user current screen again if internet connection is there.


Solution

  • I resolved my issue with following code. i am not sure whether my answer is right or wrong.

    @objc func reachabilityStatusChanged(_ sender: NSNotification) {
    
            var remoteHostStatus = self.reachability!.currentReachabilityStatus()
    
            if (remoteHostStatus == NotReachable)
            {
                print ("no net")
                let testController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "OfflineViewController") as! OfflineViewController
                window!.rootViewController = testController
                window!.makeKeyAndVisible()
            }
            else
            {
              UserTypeValidateCall()
    
            }
    
        }.
    
    //MARK:- Validating User Type
    func  UserTypeValidateCall()
    {
        let Usertype  =  UserDefaults.standard.string(forKey: "Usertype")
        print("Usertype-----",Usertype)
    
        if(( Usertype) != nil)
        {
            if (Usertype?.isEqual("Parent"))!
            {
                ParentDashBoardEnter()
            }
            else  if (Usertype?.isEqual("Employee"))!
            {
                TeacherDashBoardEnter()
            }
            else if (Usertype?.isEqual("Admin"))!
            {
                AdminDashBoardEnter()
            }
        }
        else
        {
            LoginScreenEnter()
        }
    }