简体   繁体   中英

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


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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM