How to check if WiFi is on or off in iOS Swift 2?

I want to check if the wifi is off then show alert to the user to check his/her connectivity.

I find code like this but it checks if there is an internet connection, not checking if the wifi is on or off:

  • How to load thousands records to Realm correctly?
  • How do I access a nested JSON value using Alamofire and SwiftyJSON?
  • Sorting NSArray containing NSDate objects
  • NSNotificationCenter addObserver in Swift while call a private method
  • Print appended struct (swift4)
  • iOS 9.3 : An SSL error has occurred and a secure connection to the server cannot be made
  • func isConnectionAvailble()->Bool{
    
      var rechability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "www.apple.com").takeRetainedValue()
    
      var flags : SCNetworkReachabilityFlags = 0
    
      if SCNetworkReachabilityGetFlags(rechability, &flags) == 0
      {
         return false
      }
    
      let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0
      let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
      return (isReachable && !needsConnection)
    }
    

    3 Solutions Collect From Internet About “How to check if WiFi is on or off in iOS Swift 2?”

    You can’t.

    With reachability, you can distinguish three things: a. You have a WiFi connection, you may or may not have a mobile data connection. b. You have a mobile data connection, but no WiFi connection. c. You have neither WiFi nor mobile data connection.

    You can’t check whether WiFi is turned off, or whether WiFi is turned off but there is no WiFi network nearby, or whether Airplane mode has been turned on.

    For mobile data, you can use the telephony class to find whether your device is capable of mobile data connections (iPhone and not iPad, and SIM card plugged in), and you can detect whether mobile data is disabled in the preferences of your application.

    You can take a look at the official Apple sample for Reachability:
    https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html

    var netStatus = reachability.currentReachabilityStatus()
    var connectionRequired = reachability.connectionRequired()
    var statusString = ""
    switch netStatus {
        case NotReachable:
            break
        case ReachableViaWWAN:
            //DATA
            break
        case ReachableViaWiFi:
            //WIFI
            break
    

    }

    You can use this method to check:

    First you import this framework:

    import SystemConfiguration.CaptiveNetwork
    
    
    func isWifiEnabled() -> Bool {
    
            var hasWiFiNetwork: Bool = false
            let interfaces: NSArray = CFBridgingRetain(CNCopySupportedInterfaces()) as! NSArray
    
            for interface  in interfaces {
               // let networkInfo = (CFBridgingRetain(CNCopyCurrentNetworkInfo(((interface) as! CFString))) as! NSDictionary)
                let networkInfo: [AnyHashable: Any]? = CFBridgingRetain(CNCopyCurrentNetworkInfo(((interface) as! CFString))) as? [AnyHashable : Any]
                if (networkInfo != nil) {
                    hasWiFiNetwork = true
                    break
                }
            }
            return hasWiFiNetwork;
        }