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

Deadly 提交于 2019-12-21 04:41:59

问题



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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5819088/how-to-find-server-is-reachable-with-reachability-code-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!