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;
}
}
}
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;}
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;}
}
What I am trying to achieve, is monitor any internet connectivity? So the phone needs an active connection to the internet. If it does display a UIAlertView with the option to Try Again (try the connection again to see if it has changed).
I am trying to use Reachability and connection to the api.parse.com link.
In my AppDelegate I call the setup of Reachability like this:
// Use Reachability to monitor connectivity
[self monitorReachability];
The monitorReachability is setup like this:
- (void)monitorReachability {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityChanged:) name:ReachabilityChangedNotification object:nil];
self.hostReach = [Reachability reachabilityWithHostName: #"api.parse.com"];
[self.hostReach startNotifier];
self.internetReach = [Reachability reachabilityForInternetConnection];
[self.internetReach startNotifier];
self.wifiReach = [Reachability reachabilityForLocalWiFi];
[self.wifiReach startNotifier];
}
I also have the reachability changed method as follows:
EDIT - updated method
- (void)reachabilityChanged:(NSNotification* )note {
Reachability *curReach = (Reachability *)[note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NSLog(#"Reachability changed: %#", curReach);
networkStatus = [curReach currentReachabilityStatus];
if (networkStatus == NotReachable) {
NSLog(#"NOT REACHABLE");
return;
} else {
NSLog(#"REACHABLE");
}
What I am trying to understand is the responses back. From the above it looks like I have a pointer to the current status and I am not sure how to use this. Basically I want an if statement to check if that link is reachable via the internet connection, if its not I can through an AlertView. I can then setup a boolean for the UIAlertView to use i.e. showingConnectionAlert, which can then be brought down when the connection is changed and picked up. I am unsure where to put this too.
One of the simplest ways to use the Reachability class is to import the Reachability.h into your rootViewController or whatever one is going to need the connection, then simply run this code...
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus == NotReachable) {
NSLog(#"No internet connection!");
//Alert View in here
}
else {
//Do something in here with the connecion e.g:
[self performSelector:#selector(startNSURLRequest) withObject:nil afterDelay:30.0];
}
That should simplify the process a bit.. let me know how you get on. T
I am using Reachability in my IOS app to determine a connection.
Following from this post wifi on iphone simulator
If the wifi is turned off the internet connection for the simulator is not available but the phone is still connected to Wifi therefore a connection has not changed. This is all fine and understood and of course I can test on the device itself.
However, I am looking to handle the error that a user is connected to wifi but the wifi has no internet connection like the screenshot below.
I use reachability in the following way:
#pragma mark - ()
- (void)monitorReachability {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityChanged:) name:ReachabilityChangedNotification object:nil];
self.hostReach = [Reachability reachabilityWithHostname: #"api.parse.com"];
[self.hostReach startNotifier];
self.internetReach = [Reachability reachabilityForInternetConnection];
[self.internetReach startNotifier];
self.wifiReach = [Reachability reachabilityForLocalWiFi];
[self.wifiReach startNotifier];
}
//Called by Reachability whenever status changes.
- (void)reachabilityChanged:(NSNotification* )note {
Reachability *curReach = (Reachability *)[note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NSLog(#"Reachability changed: %#", curReach);
networkStatus = [curReach currentReachabilityStatus];
}
I am using Reachability for ARC from tonymillion's github here: https://github.com/tonymillion/Reachability
Does anyone know how I can handle this situation of connection with a better error?
create following method in appdelegate to use it on any class.
-(BOOL)isHostAvailable
{
//return NO; // force for offline testing
Reachability *hostReach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [hostReach currentReachabilityStatus];
return !(netStatus == NotReachable);
}