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

Mohammed Saleh

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:

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)
}
gnasher729

You can't.

With Apple's reachability class, you can distinguish three things according to the NetworkStatus struct:

typedef enum : NSInteger {
  NotReachable = 0,         // 1
  ReachableViaWiFi,         // 2
  ReachableViaWWAN          // 3
} NetworkStatus;
  1. You have neither WiFi nor mobile data connection.
  2. You have a WiFi connection, but you may or may not have a mobile data connection.
  3. You have a mobile data connection, but no WiFi connection.

You can't check whether WiFi is turned off, or whether WiFi is turned on 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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to turn flashlight ON and OFF in swift?

iOS: How to check if UIViewControllers are unloading? (Swift)

How to check if NSDictionary is not nil in Swift 2

How to turn the iPhone camera flash on/off swift 2?

How to programatically check if orientation is locked on iOS - Swift

How to detect if iOS Wifi hardware is ON swift 3

How to turn on and off notifications in iOS?

Swift: How to check if my app has WLAN permission and the device is connected to a WIFI source?

Swift iOS -In ViewWIllAppear How to Check If ImagePicker is Being Presented or Dismissed While Switching Tabs or The View Is Being Pushed On/Off?

How to check table count and use it in an if-statement in swift iOS

Swift iOS -CollectionView how to scroll cells off of screen

iOS Swift: How to check if port is open

Xamarin iOS how to check if Location Services is off or app level device location is off

How to check the status of BlueTooth adapter (ON/OFF) with Delphi 10.3 (Firemonkey) App for both Android and iOS

How to switch off wifi on startup or from the console

How to check the swap is on or off

How to check if monitor is on or off

How to check which class an item is in Swift 2?

Android - How to turn off Wifi-direct

How to connect with Wifi Camera ios

IOS Swift 2 - How to construct multidimensional array

How to check 3G,4G and wifi internet connection in swift 2.2

How i share my Wifi using application swift in iOS

How to check if the wifi network is available or not

IOS (Swift 3) How to trigger broadcast receiver when gps is turn on/off?

How to measure WiFi dBm strength on iPhone X iOS Swift

How to check if the GPS is ON or OFF

How to get SSID of currently connected Wifi Network in swift | iOS 14?

Xcode-Swift 5: Is it possible to turn off the GPS requirement when launching iOS app on a WiFi only device?