[英]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.我有客户端服务器说“http://abc.com”,我想检查该服务器是否响应。
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.正如我看到的代码,但无法获得在哪里写我的客户的 url 来检查这个。
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.使用reachabilityWithHostName 时,您必须记住这只是主机名,不应有http:// 前缀等。
In order to check the reachability of your server through Apple (Reachability) code, you've to look around it's utility methods as below;为了通过Apple(Reachability)代码检查服务器的可达性,您必须查看它的实用方法,如下所示;
+ (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.但在我看来,我将首先在同一实例中检查其他因素组合的可达性(检查互联网连接 3G/Edge/Wifi,然后验证所需主机可达)。 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.其中“ kReachibility_ping_uri ”是包含主机名的常量,每当 iphone 客户端上的可达性发生变化时,可达性 class 将发布通知“ kReachabilityChangedNotification ”,同时您必须执行所有可达性检查并更新您的可达性状态。
If you need the example, how did i used in my apps then let me know I will provide the code.如果你需要这个例子,我是如何在我的应用程序中使用的,然后让我知道我会提供代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.