0

I'm using the Reachability class to monitor network connection and server availability in a couple of iPhone apps. I have the following code in my 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];

When I profile one of the apps (a dedicated blog reader for my companies blog) using Instruments, I see a difference of +5.9 Mb of network traffic over a one minute period when I turn on the notifiers, vs commenting those lines out.

What is the proper way to check for network connection before sending a request to a server? I probably shouldn't be monitoring the connection full time, but only when I need to know.

My other app communicates with a web service API to post pages and video, and I want to know if I have a connection to that service before attempting to post, but I want to minimize network traffic where I can.

I'd guess that this is one thing Apple looks for when approving/rejecting apps in the app store.

Alpinista
  • 3,751
  • 5
  • 35
  • 46
  • I just used the technique suggested in this [SO][1]. [1]: http://stackoverflow.com/questions/7015644/ios-reachability-not-recognizing-when-host-is-removed/7016594#comment9733939_7016594 – user523234 Nov 11 '11 at 00:49
  • try using AFNetworking it has a built in support for detecting the server status and it uses blocks do that ... check it out ! – ahmad Jul 15 '12 at 06:38

1 Answers1

1

Instead of checking the connection before you make the request, just check for a network connection error when you do make the request. You can periodically make requests if you want to update the status of the network.

Randall
  • 14,691
  • 7
  • 40
  • 60