title: Swift iOS- What to do when there is a wifi connection but no internet connection?
body: <p>I'm using Alamofire in my iOS app. I use bool values in viewWillAppear and in AppDelegate with NSNotifications to check if there is an internet connection. If there is no wifi connection a pop up appears to inform the user. If there is a wifi connection the pop up disappears and everything works fine again. I've had no problems as long as wifi is clearly not working.</p>
  
  <p>I was at a meetup and someone explained to me that the way it works is it it looks for a wifi connection and not an internet connection. For e.g.. if I have a wifi router and it's plugged in but the router isn't connected to the internet Alamofire will view this as a successful connection because it actually is connecting to wifi although it doesn't know the wifi can't connect to the internet.</p>
  
  <p>I was just in a situation where I connected to an open network, my app initially responded as if I were actually connected to the internet (no pop up) but I couldn't get connect to anything. The wifi signal was on full. In terminal I ran a ping and it turns out the connection was dead. My app couldn't tell the difference.</p>
  
  <p><a href="https://i.stack.imgur.com/lHjTC.png" rel="noreferrer"><img src="https://i.stack.imgur.com/lHjTC.png" alt="enter image description here"></a>  <a href="https://i.stack.imgur.com/JcemC.png" rel="noreferrer"><img src="https://i.stack.imgur.com/JcemC.png" alt="enter image description here"></a></p>
  
  <p><strong>How do I make a pop up appear in a sitaution like this?</strong></p>
  
  <p><strong>Also what is .case unknown for?</strong></p>
  
  <p>Alamofire.Swift:</p>
  
  <pre><code>import Foundation
  import Alamofire
  
  open class NetworkManager {
  
      open static var sharedManager: NetworkReachabilityManager = {
  
          let reachabilityManager = NetworkReachabilityManager()
  
          reachabilityManager?.listener = { (status) in
  
              switch status {
  
              case .notReachable:
                  print("The network is not reachable")
                  NotificationCenter.default.post(name: NSNotification.Name(rawValue: "unsuccessful"), object: nil)
  
              case .unknown : //???????
                  print("It is unknown wether the network is reachable")
                  //I'm not sure whether to put a Notification for successful or unsuccessful???
  
              case .reachable(.ethernetOrWiFi):
                  print("The network is reachable over the WiFi connection")
                  NotificationCenter.default.post(name: NSNotification.Name(rawValue: "successful"), object: nil)
  
              case .reachable(.wwan):
                  print("The network is reachable over the WWAN connection")
                  NotificationCenter.default.post(name: NSNotification.Name(rawValue: "successful"), object: nil)
              }
          }
  
          reachabilityManager?.startListening()
          return reachabilityManager!
      }()
  }
  </code></pre>
  
  <p>AppDelegate:</p>
  
  <pre><code>func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -&gt; Bool {
  
          NetworkManager.sharedManager.startListening()
  </code></pre>
  
  <p>SomeVC:</p>
  
  <pre><code>override func viewWillAppear() {
          super.viewWillAppear()
  
          NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(successful), name: "successful", object: nil)
  
          NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(unsuccessful), name: "unsuccessful", object: nil)
  
         if NetworkManager.sharedManager.isReachable == true{
              self.successful()
         }else{
              self.unsuccessful()
         }
  
         if NetworkManager.sharedManager.isReachableOnWWAN == true{ 
              self.successful()
         }else{
              self.unsuccessful()
         }
  
         if NetworkManager.sharedManager.isReachableOnEthernetOrWiFi == true{ 
              self.successful()
         }else{
              self.unsuccessful()
         }
  }
  
  func successful(){
      //dismiss pop up
  }
  
  func unsuccessful(){
      //show pop up
  }
  
  deinit{
      NSNotificationCenter.defaultCenter().removeObserver(self, name: "successful", object: nil)
      NSNotificationCenter.defaultCenter().removeObserver(self, name: "unsuccessful", object: nil)
  }
  }
  </code></pre>