What should be verified first - hostReachability or netReachability? - iphone

Here i have a very interesting topic for discussion or you can say i need a suggestion for the better approach.
Here is my code
+ (BOOL) isConnected
{
BOOL flag = TRUE;
if (![self isHostReachable])
{
flag = FALSE;
NSString* alertTitle= #"";
NSString* alertMessage= #"";
if (![self isInternetReachable])
{
alertTitle = #"Network unavailable";
alertMessage = #"We can't connect to the Internet. Check your settings/connection.";
}
else
{
alertTitle = #"Server not responding";
alertMessage = #"Server not responding at the moment. Please try again later. Sorry for inconvenience";
}
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:self
cancelButtonTitle:#"Close"
otherButtonTitles:nil];
[alert show];
}
return flag;
}
+ (BOOL) isInternetReachable
{
Reachability *netReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus netSat = [netReachability currentReachabilityStatus];
return (!(netSat == NotReachable));
}
+ (BOOL) isHostReachable
{
Reachability *hostReachability = [Reachability reachabilityWithHostname: [Connection returnHostName]];
NetworkStatus netSat = [hostReachability currentReachabilityStatus];
return (!(netSat == NotReachable));
}
In my code, I used to call the "isConnected" method in order to check the connectivity status, before asking my server for data.
In the method, I am checking the hostReachability first of all. My thinking behind doing this is to save the computation time.
If the hostReachability returns true it implies netConnectivity is also there(no need to verify). So here we are done with just one computation.
If the hostReachability returns false, then I am checking is it the net connectivity thats responsible for it. Hence two computations.
But it usually seen that the code first verifies the netConnectivity, if its ok, then verify the hostReachability.
So most of the times Two computations, thats in contrast with my way of getting the thing done.
Please suggest which one you think is better approach?

Well netConnectivity would verify if the network is in position and not down due to any of the 100 reasons, then the host is verified to be reachable as the server may be down due to some issue.
If the network is verified as failed connection it will rule out verifying the host in the first place. vice-versa is not true though..
I hope that was useful as explanation to you.
Cheers!!

Related

Issue in loading http://www.google.com url in iOS app

I was using the following code for checking the internet connection in my first iOS app (almost 3 years ago).
NSError *err = nil;
NSStringEncoding encoding;
NSString * connectionstring = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.google.com"] usedEncoding:&encoding error:&err];
if(connectionstring.length ==0)
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Error Occured" message:#"No Internet Connection." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
// loading another screen
}
The problem is in India and US it is going into the else condition but in Belgium it is going into the if condition and throwing the error. So are there any restrictions for Google in Belgium?
If it is tagged incorrectly please guide me. In my current versions solved this issue by using Rechability classes to check this . Just I wanted to know about it . Thanks in advance.
As a first step, look at the encoding.
You can log the encoding right after the connectionstring is created by inserting
NSLog(#"encoding: %#", [NSString localizedNameOfStringEncoding:encoding]);
into your code.
For further debugging, you could try loading the url into an NSData object first, then inspect the data and finally create a NSString from the data.
Alternative solution: To check internet connection Use Reachability class provided by apple.
Download Apple Sample Here, add Reachability class to your project.
- (BOOL)isConnected
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
Instead of using google.com use any local client server test url for internet checking it will also be very fast responsive comparatively of any other way of internet validation.

NSStreamEventErrorOccurred triggered even if the network is pretty strong signal

I have implemented the NSStream delegate. I have implemented the same as Witap Application
In that, I have implemented handleEvent delegate
- (void) stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode{
switch(eventCode) {
case NSStreamEventOpenCompleted:
{
[tcpServer release];
tcpServer = nil;
break;
}
case NSStreamEventHasBytesAvailable:
{
//done my stuff here
}
case NSStreamEventErrorOccurred:
{
UIAlertView *wifiLostAlert = [[UIAlertView alloc] initWithTitle:#"Wifi
connection error" message:#"" delegate:nil cancelButtonTitle:#"Continue" otherButtonTitles:nil];
[wifiLostAlert show];
[wifiLostAlert release];
wifiLostAlert = nil;
}
}
In my client site, they reported an issue as
"Multiple time when we was either trying to send a message to the
learner or synching devices she got the following message: “Wifi
connection error.
Same happend at 2 different networks and it doesnot recover quickly
even if the network is pretty strong signal."
Unfortunately, I am unable to reproduce this issue in my site and it's working fine in another client site too!!.
Any clue's regarding the issue. Any help on this is appreciated.
Thank you.
I would recommend you supply the client with an updated version that displays more information about what error is actually occurring.
You can get more information about the error using:
NSError* error = [stream streamError];
You can find more information about NSError at NSError Class Reference.
Something like this might work in your case:
NSString* errorMessage = [NSString stringWithFormat:#"%# (Code = %d")",
[error localizedDescription],
[error code]];
Then change your UIAlertView to this:
UIAlertView *wifiLostAlert = [[UIAlertView alloc]
initWithTitle:#"Stream Error"
message:errorMessage
delegate:nil
cancelButtonTitle:#"Continue"
otherButtonTitles:nil];
This won't solve the problem but will give both you and your client more information about the root cause is.
For example, you may find the error is "Connection refused." which would point to a problem not with WiFi signal strength but in the server-side software.
Good luck!

Reachability False Positive

I'm getting false positives with my Reachability code. I get the UIAlert "No Internet Connection", when the network is available but not activated. Any help is very much appreciated.
//Check for Internet
Reachability* wifiReach = [[Reachability reachabilityWithHostName: #"www.apple.com"] retain];
NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
switch (netStatus)
{
case NotReachable:
{
NSLog(#"Access Not Available");
break;
}
case ReachableViaWWAN:
{
NSLog(#"Reachable WWAN");
break;
}
case ReachableViaWiFi:
{
NSLog(#"Reachable WiFi");
break;
}
}
if (netStatus==NotReachable){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Internet Required" message:#"There is no internet connection. Please connect and try again" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}else{
//Push Controller
}
Same answer as the previous question.
Reachability sometimes only gives the correct answer after your app, or some previous app, has tried to connect and waited for data. So you might as well just try to get data from your connection, as that may well give you a more correct answer more quickly than asking the Reachability API. Maybe just let the user decide an activity indicator has been spinning long enough.

How to check network connection in iphone

this code is to check if internet connection is available or not.If internet connection is available then username and password should get validated from server database i.e send request method should get called and if not then username and password should get validated from local database i.e check method should get called.but the prob here is when the internet is off then also it gets into the send request method instead of getting in check nethod.What may be the prob.Please help me in solving this problem. i have added reachability files and imported the CFNetwork.framework.
- (void) showNetWorkAlert {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
UIAlertView *networkAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Network connection unavailable."
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Retry", nil];
[networkAlert show];
[networkAlert release];
}
#pragma mark To Check Network Connection.
- (BOOL) currentNetworkStatus {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
BOOL connected;
const char *host = "www.apple.com";
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host);
SCNetworkReachabilityFlags flags;
connected = SCNetworkReachabilityGetFlags(reachability, &flags);
BOOL isConnected = YES;
isConnected = connected && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
CFRelease(reachability);
if(!isConnected) {
// sleep(1);
[self showNetWorkAlert];
//[self check];
}
else
return isConnected;
//[self sendRequest];
return isConnected;
}
-(IBAction)buttonPressed:(id)sender
{
//[self sendRequest];
//[[Reachability sharedReachability] setHostName:kHostName];
//Set Reachability class to notifiy app when the network status changes.
//[[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];
//Set a method to be called when a notification is sent.
//[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityChanged:) name:#"kNetworkReachabilityChangedNotification" object:nil];
//[self updateStatus];
//[self sendRequest];
//NSLog(<#NSString *format#>)
//this is to select username and password from database.
//[self check];
if ([self currentNetworkStatus]) {
[self sendRequest];
}
else {
[self check];
}
}
Rocky your code is right .Just check the URL is proper or not .
there is other way also
On button check the host like this
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
hostReach = [[Reachability reachabilityWithHostName:#"www.google.com"]retain];
[hostReach startNotifier];
and in the (reachabilityChanged:) method call make like this
-(void)reachabilityChanged:(NSNotification*)note
{
static BOOL showNotConnnected =NO;
Reachability *curReach =[note object];
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
NetworkStatus netStatus = [curReach currentReachabilityStatus];
if(netStatus != NotReachable)
{
UIAlertView *notconnect1 = [[UIAlertView alloc]initWithTitle:#"Server is connected" message:#"Server is connected" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:#"Cancel",nil];
[notconnect1 show];
[notconnect1 release];
}
else if(showNotConnnected == NO)
{
showNotConnnected =YES;
UIAlertView *notconnect = [[UIAlertView alloc]initWithTitle:#"Server is Not connected" message:#"Server may be slow or not connected" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:#"Cancel",nil];
[notconnect show];
[notconnect release];
}
}
I hope this will help you
I do think there is a sample code called Reachability from apple can help you. see the URL below
http://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html
You should look at Apple's reachability code to check for a connection to the Internet. There is a sample project you can get from them in the iOS Developer Portal.
Reachability should not be used to detect if the network is available before making a network request because the act of making a network request can bring up the network if needed. Reachability should only be used to detect when the network becomes available after being unavailable.
Just try making the network request -- you'll get an error back if the network is unavailable. Usually the response will come back right away, but if the network is spotty it can take a while, so you shouldn't ever make synchronous network calls on the main thread. Use NSURLConnection if possible and you'll get callbacks when something happens.

Reachability Help - WiFi Detection

I have imported Reachability into my application, and I have a couple of how-to questions for you all. Let me explain my application and other tools first.
This application communicates with two things AT THE SAME TIME, an ad-hoc network, and the internet through 3G. Note: The ad-hoc network IS NOT connected to the internet. This works perfectly - it's already implemented and tests out wonderfully.
With that being said, I want to implement Reachability to detect a two things.
1) Is the user connected to a wifi ad-hoc network? (Even better, if possible, is to detect if it is connected to the wifi ad-hoc network with a prefix of WXYZ. For example, if there are two networks listed, one called Linksys and the other called WXYZ-Testing_Platform, it knows whether or not it is connected to WXYZ).
2) Can the user connect to the internet through 3G (or 2G, etc) and access our server?
Thanks in advance
EDIT TO INCLUDE ANSWER FOR FUTURE LOOKERS:
For 1), my code looks like this:
.h
#import <SystemConfiguration/CaptiveNetwork.h> //for checking wifi network prefix
.m
- (BOOL) connectedToWifi
{
CFArrayRef myArray = CNCopySupportedInterfaces();
// Get the dictionary containing the captive network infomation
CFDictionaryRef captiveNtwrkDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
NSLog(#"Information of the network we're connected to: %#", captiveNtwrkDict);
NSDictionary *dict = (__bridge NSDictionary*) captiveNtwrkDict;
NSString* ssid = [dict objectForKey:#"SSID"];
if ([ssid rangeOfString:#"WXYZ"].location == NSNotFound || ssid == NULL)
{
return false;
}
else
{
return true;
}
}
And for 2), I imported Reachability and have it using this method whenever I go to connect to the server... NOTE:replace http://www.google.com with the server information
-(void) checkIfCanReachServer
{
UIAlertView *errorView;
Reachability *r = [Reachability reachabilityWithHostName:#"http://www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if(internetStatus == NotReachable) {
errorView = [[UIAlertView alloc]
initWithTitle: #"Network Error"
message: #"Cannot connect to the server."
delegate: self
cancelButtonTitle: #"OK" otherButtonTitles: nil];
[errorView show];
}
}
Reachability only lets you know if the device can send data packets out successfully. So
for 1) you should refer to iPhone get SSID without private library. For 2) you will use Reachability only to check for an internet connection then you would need to use NSURLConnection or other network library to make sure you can reach your server.