Hi I have read some posts and see that I can use "reachability" sample from Apple to check if the current device is run on Wi-Fi or cellular and decide if a connection can be fired. However I have a lots of webviews and HTTP calls in my app on different classes, so I am afraid I have to call the check in every single method which will start a connection. I would like to ask if there is any way to check the status of the network and disallow all traffic on cellular? Thanks a lot.
Asked
Active
Viewed 192 times
0
-
Reachability allows you to observe a connection change NSNotification "kReachabilityChangedNotification", which eliminates this problem. – Mick MacCallum Nov 13 '12 at 04:12
1 Answers
0
You are looking for the ReachableViaWiFi network status, which is broadcast via NSNotification. You can setup that up in your code like this:
@property (nonatomic, assign) NetworkStatus currentNetStatus;
...
- (void) startListeningForWifi{
Reachability* hostReach = [Reachability reachabilityWithHostName:@"hostName"];
[hostReach startNotifier];
// reachability set up
// 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];
}
- (void)reachabilityChanged: (NSNotification* )note{
Reachability *curReach = [note object];
self.currentNetStatus = [curReach currentReachabilityStatus];
}
Then it's easy to check the currentNetStatus
before you make a network call. If that is not equal to ReachableViaWiFi
then you are not on wifi.

foggzilla
- 1,705
- 11
- 15
-
thanks for your answer its a way to solve my problem. But the case is a little more complicated, say currently I have a connection downloading a file and the network status changes from wifi to cellular, am i able to cancel the connection right away? – user1653545 Nov 13 '12 at 06:09
-
you cancel requests by following this [post](http://stackoverflow.com/questions/11070175/how-to-cancel-network-request-with-afnetworking) – foggzilla Nov 13 '12 at 14:40
-
-
thanks a lot. I havent tried yet because I used another approach for this problem. – user1653545 Nov 16 '12 at 06:35