1

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.

Ram
  • 670
  • 1
  • 10
  • 19

4 Answers4

1

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()
    }
}
Ram
  • 670
  • 1
  • 10
  • 19
0

Same way use this function in your project and navigate the user to the particular view controller.

For more detail please refer this link. Check for internet connection with Swift

swapnil patel
  • 374
  • 2
  • 15
0
import UIKit
import Reachability
class CommonUtility: NSObject {

    class func isConnected() -> Bool{
        let reachability:Reachability = Reachability.forInternetConnection()
        let networkStatus = reachability.currentReachabilityStatus().rawValue
        return networkStatus != 0
   }
}
Nirav Hathi
  • 1,137
  • 2
  • 8
  • 20
0

//Use below link download it and just drag and drop Reachability Folder to your project

https://github.com/ashleymills/Reachability.swift

or

pod 'ReachabilitySwift', '~> 4.1'

Note : Here ReachabilitySwift version is based on swift version you have to insatll

let reachability = Reachability()!

  override func viewDidLoad()
  {
    super.viewDidLoad()
  }


 if (reachability.isReachable)
   {
         // your code
   }
 else
   {
       // your code
   }
  • hi sorry for the late reply . i should check reachability for all classes or else any other option to check directly in appdelegate – Ram Dec 26 '17 at 10:28
  • you have to check for all classes (i mean where you want) , there is no option in appdelegate . – Vadlapalli Masthan Dec 27 '17 at 12:28