2011-11-10 9 views
0

我使用的可达性类监测一对夫妇iPhone应用程序的网络连接和服务器可用性的网络连接。我在我的AppDelegate下面的代码:有道检查iOS中

// Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the 
// method "reachabilityChanged" will be called. 
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil]; 

// Start checking for reachability to myWebService.com 
//Change the host name here to change the server your monitoring 
hostReach = [Reachability reachabilityWithHostName: @"myWebService.com"]; 
[hostReach startNotifier]; 

internetReach = [Reachability reachabilityForInternetConnection]; 
[internetReach startNotifier]; 

wifiReach = [Reachability reachabilityForLocalWiFi]; 
[wifiReach startNotifier]; 

当我使用配置文件的工具软件之一(一个专门的博客的读者对我的企业博客),我看到了一个网络流量+5.9 MB的差异当我打开通知程序时,与评论这些行。

什么是检查向服务器发送请求之前的网络连接的正确方法?我可能不应该全天监控连接,但只有当我需要知道时。

我的其他应用程序与Web服务API通信发布的网页和视频,我想知道有没有尝试发布之前,该服务的连接,但我想,我可以最大限度地减少网络流量。

我猜,这是一件事苹果看起来批准/在App Store拒绝的应用程序时。

+0

我刚刚使用了本文[SO] [1]中提出的技术。 [1]:http://stackoverflow.com/questions/7015644/ios-reachability-not-recognizing-when-host-is-removed/7016594#comment9733939_7016594 – user523234

+0

尝试使用AFNetworking它有一个内置的支持检测服务器状态,并使用块做到这一点...检查出来! – ahmad

回答

1

而不是检查连接您做出请求之前,只需检查网络连接错误,当你作出的要求。如果您想更新网络的状态,您可以定期发出请求。