Is it possible to query WiFi state (enabled/disabled) on iOS programmatically? The query should return true when WiFi is enabled and device is not connected to any network.
EDIT: I am aware of the functionality provided by Reachability class and as far as I understand it does not recognize enabled but not connected state of WIFI. I.e. the following code will return NetworkStatus NotReachable, which is not what I need.
Reachability* r = [Reachability reachabilityForLocalWiFi];
NetworkStatus ns = [r currentReachabilityStatus];
Disclaimer: The following solution is not robust and there is no guarantee it will pass AppStore.
The only viable solution I was able to find so far is to request and evaluate a list of available interfaces using getifaddrs function. The list looks differently in case WiFi disabled/enabled/connected:
NSCountedSet * cset = [NSCountedSet new];
struct ifaddrs *interfaces;
if( ! getifaddrs(&interfaces) ) {
for( struct ifaddrs *interface = interfaces; interface; interface = interface->ifa_next) {
if ( (interface->ifa_flags & IFF_UP) == IFF_UP ) {
[cset addObject:[NSString stringWithUTF8String:interface->ifa_name]];
}
}
}
freeifaddrs(interfaces);
return [cset countForObject:#"awdl0"] > 1 ? WIFI_ON : WIFI_OFF;
You can use Reachability to check this. Import the files, then you can do this:
Reachability *networkReachability = [Reachability reachabilityWithHostName:#"http://google.com];
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
if (networkStatus == ReachableViaWiFi) {
//wifi
}
You could use the Reachability class that apple has provided here then check for this:
[Reachability reachabilityForLocalWiFi];
Related
I want to be able to check to see if the user is connected to WiFi, but not connected to a network. So basically I want to check the state of the WiFi button on the device setting page to check if button is enabled or disabled.
At the moment I can check to see if the Wifi is connected to a network or not connected to an network doing the following:
BOOL hasWiFiNetwork = NO;
NSArray *interfaces = CFBridgingRelease(CNCopySupportedInterfaces());
for (NSString *interface in interfaces)
{
NSDictionary *networkInfo = CFBridgingRelease(CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interface)));
if (networkInfo != NULL)
{
hasWiFiNetwork = YES;
break;
}
else
{
hasWiFiNetwork = NO;
break;
}
}
Try this code and use Reachability class.
BOOL isConnectedProperly = ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWiFi);
Update
It turns out that it isn't possible, there is no API/Framework/BOOL value that can do this because Apple havn't added any kind of ability to check to see if the WiFi is switched on or off for developers. As explained nicely here: https://stackoverflow.com/a/12906461/4657588
Then this SO post should be what you want: https://stackoverflow.com/a/7938778/4657588
Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus status = [reachability currentReachabilityStatus];
if (status == ReachableViaWiFi) {
// WiFi
}
else {
// Either WiFi is off or we are not connected to a WiFi network.
}
First you need to download reachability file from apple developer site
and after that add these piece of code every time where ever yu want to check.
-(BOOL)isConnectedTointernet{
BOOL status = NO;
Reachability *reachability = [Reachability reachabilityForInternetConnection];
int networkStatus = reachability.currentReachabilityStatus;
status = (networkStatus != NotReachable)? YES:NO;
return status;}
I am checking the status of internet reachability using class "Reachability". But while testing, if I am setting 100% packet loss in developer settings, still I get reachability status as "ReachableViaWiFi". I am confused what's happening. Shouldn't it be "NotReachable" in that situation ?
Here is my code snippet:
Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
if(networkStatus == NotReachable){
NSLog(#"NotReachable");
}
else if(networkStatus == ReachableViaWiFi){
NSLog(#"ReachableViaWiFi");
}
else if(networkStatus == ReachableViaWWAN){
NSLog(#"ReachableViaWWAN");
}
Is there any other way that give me status as FALSE in this situation?
The number of packets that you lose doesn't influence reachability. After all, this could be just momentary (you took your phone into a shielded room, or a heavy electrical motor was just turned on). Reachability is about your WiFi, or 3G, or Ethernet on a Mac, being turned on. It's not about the quality of the connection.
This worked for me:
-(BOOL)connected
{
Reachability *r = [Reachability reachabilityWithHostName:#"www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
{
return NO;
}
else
{
return YES;
}
}
Can i check the state of wifi service. Is it enable or disable if user phone not connected to WiFi. I need it for create user alert, because i need WIFi state at "ON" state for my app with navigation.
For this you need to import reachability classes in your project.
BOOL isConnectedProperly = ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWiFi);
Ref:
https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html
https://github.com/tonymillion/Reachability
Use Reachability Class for checking the network connection state
Reachability *internetReachable=[Reachability reachabilityForInternetConnection];
if(internetReachable.currentReachabilityStatus == ReachableViaWiFi)
{
wifiAvailable=YES;
}
else
{
// Show the alert to turn on wifi
}
Using the code apple provide https://developer.apple.com/iphone/library/samplecode/Reachability
Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus status = [reachability currentReachabilityStatus];
if(status == NotReachable)
{
//No internet
}
else if (status == ReachableViaWiFi)
{
//WiFi
}
else if (status == ReachableViaWWAN)
{
//3G
}
I wrote a connection checker using Apple's Reachability class' reachabilityWithHostName: method. Here is my code.
-(BOOL)checkConnection{
Reachability *reachability = [Reachability reachabilityWithHostName:#"www.example.com"];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if (remoteHostStatus != NotReachable) {
return YES;
}
else {return NO;}
}
SO here is the use cases:
if I have wifi connection** : returns YES as expected.
If I have cellular connection : returns YES as expected.
If cellular and Wifi is disabled : returns NO as expected.
If I have WiFi connection; but the DSL cable is unplugged (so the host shouldn't
be reachable, internet connection is not available.) : returns YES and it's unexpected.
Also if cellular is enabled but at my current position I have no signal : returns YES and it's unexpected.
How can I solve these unexpected results?
Thank you.
With #Martin Koles' help I added a html file into the server. It only has a random value inside. Now I check reachability. If server is reachable, I am trying to get the value from html file. Than, if I could get the value returning YES. If I couldn't (the serverValue should be nil) returning NO..
-(BOOL)checkConnection{
Reachability *reachability = [Reachability reachabilityWithHostName:#"www.izmirmobil.com"];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if (remoteHostStatus != NotReachable) {
NSURL *url = [NSURL URLWithString:#"http://www.example.com/getAValue.html"];
NSError *errr = nil;
NSStringEncoding enc;
NSString *serverValue = [[NSString alloc] initWithContentsOfURL:url usedEncoding:&enc error:&errr];
if(serverValue)return YES;
else return NO;
}
else {return NO;}
}
I have create a method to check if the host is reachable. I had download the Reachability class (both .h & .m) from apple developer websites and import to my project. I have pass the NSString Name as the URL (host Name). The hostName is http://www.google.com. However, no matter what host name I pass to this method and its always return NO (connectToHost). The code as below:
- (BOOL) checkHostAvailability:(NSString *)Name{
BOOL connectToHost;
hostReach = [[Reachability reachabilityWithHostName:Name] retain];
[hostReach startNotifier];
NetworkStatus hostStatus = [hostReach currentReachabilityStatus];
NSLog(#"Name: %#", Name);
NSLog(#"hostStatus is %#", hostStatus);
if(hostStatus == NotReachable){
NSLog(#"Here is the checkHostAvailability Method and host NOT reachable");
connectToHost = NO;
}else{
NSLog(#"Here is the checkHostAvailability Method and host is reachable");
connectToHost = YES;
}
return connectToHost;
}
After a few hours of investigation, I have found out that the NetworkStatus hostStatus always equal to null. I assume this is why this method is not working. And I have spend 8 hours to find out the problem with this code and search out this website, however I still couldn't find the problem and solution.
Please help and much appreciated.
Remove the 'http://' from the host name.
If you wanted to use http://www.google.com as your host, you would pass 'google.com' as the hostname. Do not include http://, the ending slash or anything that could follow an ending slash. www. is fine to include.
[Reachability reachabilityWithHostName:#"google.com"];
This is the code I use for all the connection checks (see here: iOS Developer Library -Reachability) and it is working fine for me :
-(BOOL) hasConnection{
//check network status _ iphone settings
Reachability *internetReachability = [Reachability reachabilityForInternetConnection];
[internetReachability startNotifier];
NetworkStatus status=[internetReachability currentReachabilityStatus];
if (status == NotReachable) {
NSLog(#"No internet");
//[internetReachability stopNotifier];
return NO;
}else {
NSLog(#"Internet is ON");
// [internetReachability stopNotifier];
//check internet connection _reachable path
Reachability *hostReachable=[Reachability reachabilityWithHostName:#"www.apple.com"];
BOOL connectionRequired =[hostReachable connectionRequired];
NSLog(#"%hhd",connectionRequired);
[hostReachable startNotifier];
Reachability *wifiReachability=[Reachability reachabilityForLocalWiFi];
[wifiReachability startNotifier];
NetworkStatus hostStatus=[hostReachable currentReachabilityStatus];
if(hostStatus == NotReachable){
NSLog(#"No internet connection");
[hostReachable stopNotifier];
return NO;
}else{
NSLog(#"Connection is ok");
[hostReachable stopNotifier];
return YES;
}
}
}