alex
alex

Reputation: 957

How can I restrict all networking requests to a Wi-Fi connection with AFNetworking 2.0?

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

Upvotes: 1

Views: 2191

Answers (3)

David Snabel-Caunt
David Snabel-Caunt

Reputation: 58361

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.

Upvotes: 4

Piyush Dubey
Piyush Dubey

Reputation: 2414

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

Upvotes: 0

nnarayann
nnarayann

Reputation: 1449

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?

Upvotes: 0

Related Questions