6


我有客户端服务器说“http://abc.com”,我想检查该服务器是否响应。
如何使用苹果的可达性代码检查这一点?
正如我看到的代码,但无法获得在哪里写我的客户的 url 来检查这一点。
请给我建议。

4

2 回答 2

9

这是一个同步示例。您可能希望异步执行此操作,但这会让您快速检查。

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 :-("];
}

使用reachabilityWithHostName 时,您必须记住这只是主机名,不应有http:// 前缀等。

于 2011-04-28T13:18:43.373 回答
0

为了通过Apple(Reachability)代码检查服务器的可达性,您必须查看它的实用方法,如下所示;

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

但在我看来,我将首先在同一实例中检查其他因素组合的可达性(检查互联网连接 3G/Edge/Wifi,然后验证所需主机可达)。在我的场景中,这只是更新可达性方法中的一项安全检查。

 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;
 }

其中“ kReachibility_ping_uri ”是包含主机名的常量,每当 iphone 客户端上的可达性发生变化时,可达性类将发布通知“ kReachabilityChangedNotification ”,同时您必须执行所有可达性检查并更新您的可达性状态。

如果你需要这个例子,我是如何在我的应用程序中使用的,然后让我知道我会提供代码。

于 2011-04-28T13:45:42.380 回答