1

I have a rather odd question. I want to know if it's possible to block any requests (current and future) using AFNetworking 2.0. My app needs to do something like this: if the user is not on a wifi connection -> block any requests that the app may have. I only found a way to block all current requests:

[self.manager.operationQueue cancelAllOperations];

Any help is welcome and much appreciated

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
alex
  • 957
  • 2
  • 9
  • 16

3 Answers3

4

Since iOS 6, NSMutableURLRequest allows you to specify whether a request can be made over a cellular connection by calling setAllowsCellularAccess:. This is explained by this Apple document. You will need to set this for every request you make.

With AFNetworking, the cleanest way to hook into AFHTTPRequestOperationManager is to use your own requestSerializer subclass and override -requestWithMethod:URLString:parameters:error: to call super and modify the request with setAllowsCellularAccess:.

If you are also using NSURLSession in your iOS 7 code paths, you can use an NSURLSessionConfiguration with its allowsCellularAccess property set to NO. This is set just once per session.

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
  • Does setting `setAllowsCellularAccess` to `NO` guarantee that communication will take place over Wi-Fi if the device has both Wi-Fi and cellular connection turned on? – iphondroid Dec 08 '14 at 14:21
  • you can just `setAllowsCellularAccess` to YES on the NSMutableURLRequest returned by `-requestWithMethod:URLString:parameters:error:` if you don't want to subclass – blueether Apr 27 '15 at 01:22
0

I don't know if it can be done using AFNetworking, but it seems that you can achieve this with custom NSURLProtocol:

Can I block network access for a specific moment?

Community
  • 1
  • 1
nnarayann
  • 1,449
  • 19
  • 24
0

Yes, it is possible using AFNetworking 2.0. Please check below links.
1. AFNetworking + cancelAllRequests
2. How to cancel network request with afnetworking

Update:-
I am sure you would also like to check this link:- AFNetworking and No Internet Connection scenario

Community
  • 1
  • 1
Piyush Dubey
  • 2,416
  • 1
  • 24
  • 39
  • thanks for your response. cancelAllOperations method only cancels all ongoing operations. What I need is to block all future requests that the user might make.My scenario is that if the user doesn't want to use cellular data (he only wants to use wifi) then I have to block any requests that the app might make until he agrees to use cellular data in this appRight now I'm using a bool value to skip any request but this has to be done for each request individually. I was looking for something of a higher level in AFNetworking 2.0 that will not permit any future request the user might make – alex Jan 22 '14 at 15:54