在iOS中检查网络连接的正确方法

在iOS中检查网络连接的正确方法,ios,ios4,ios5,Ios,Ios4,Ios5,我在几个iPhone应用程序中使用可达性类来监控网络连接和服务器可用性。我的AppDelegate中有以下代码: // Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the // method "reachabilityChanged" will be called. [[NSNotificationCenter defaultCenter] addObserver:

我在几个iPhone应用程序中使用可达性类来监控网络连接和服务器可用性。我的AppDelegate中有以下代码:

// 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通信以发布页面和视频,我想在尝试发布之前知道我是否连接到该服务,但我希望尽可能减少网络流量


我猜这是苹果在AppStore中批准/拒绝应用程序时需要考虑的一件事。

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

我刚刚使用了[1]中建议的技术。[1] :尝试使用AFNetworking它有一个用于检测服务器状态的内置支持,它使用块来实现这一点。。。过来看!