How to find server is reachable with Reachability code in iPhone?
Asked Answered
B

2

6


I have client server say "http://abc.com" and i want to check whether this server responding or not.
How to check this with Reachability code from apple?
As i see that code but not able to get where to write my client's url to check this.
Please suggest me.

Bander answered 28/4, 2011 at 13:7 Comment(0)
N
9

Here's a synchronous example. You'll probably want to do this asynchronously, but this will get you through for a quick check.

Reachability *netReach = [Reachability reachabilityWithHostName:@"host.name"];
//return [netReach currentReachabilityStatus];
NetworkStatus netStatus = [netReach currentReachabilityStatus];
if (netStatus==ReachableViaWiFi) {
    [ViewManager showStatus:@"Reachable (WiFi)!"];
} else if(netStatus==ReachableViaWWAN) {
    [ViewManager showStatus:@"Reachable (WWAN)!"];
} else {
    [ViewManager showStatus:@"Not reachable, aww :-("];
}

When using reachabilityWithHostName you have to remember that this is just the host name, there should be no http:// prefix or the like.

Nakitanalani answered 28/4, 2011 at 13:18 Comment(5)
I want to emphasize that the example above can block the main thread for a long time in certain circumstances.Crissum
@Crissum Yes indeed, hence the note that they'll probably want to do it asynchronously :)Nakitanalani
This code won't work, because Apple's host reachability doesn't do what you think it does. It only checks the first hop to the router is reachable. If that's working, it reports reachable, no matter if the actual host in question is alive and responding, or on fire.Yvonneyvonner
@Yvonneyvonner Thanks for the downvote, however it does what we think it does given the definition and request in the question for a sample using "reachability" framework. What it won't do is check if the other side is responding, but it will check that a) the host is resolvable by name, and b) that an interface is available to transmit a packet to said host based on the network configuration.Nakitanalani
I disagree. The question: "I want to check whether the server is responding or not. How to check this with Reachability code from apple?" to which the answer is "You can't". Also, sarcasm is not helpful.Yvonneyvonner
P
0

In order to check the reachability of your server through Apple (Reachability) code, you've to look around it's utility methods as below;

+ (Reachability*) reachabilityWithHostName: (NSString*) hostName;

But in my opinion I will check the reachability for combination of other factors first (check internet connection 3G/Edge/Wifi and then verify desired host reachable) within the same instance. Just one of the safe check inside the update reachability method in my scenario.

 else if (((netStatus == ReachableViaWiFi) || (netStatus == ReachableViaWWAN)) && connectionRequired == YES)
 {
       isInternetConAvailable = ([[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:kReachibility_ping_uri] 
                                                          cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0] delegate:self]) ? YES : NO;
 }

Where 'kReachibility_ping_uri' is the constant contains hostname, the reachability class will post the notification 'kReachabilityChangedNotification' whenever the reachability change on iphone-client at the same time you've to perform all the reachability checks and update your reachability status.

If you need the example, how did i used in my apps then let me know I will provide the code.

Panhandle answered 28/4, 2011 at 13:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.