How to intercept networking calls (REST) in iOS using NSURLProtocol
afnetworking, intercept, ios, networking, nsurlprotocol
Solution
OK, I borrowed some code from this really cool network and data debugging component https://github.com/square/PonyDebugger and did the job very nice and smooth.
The regarding class is https://github.com/square/PonyDebugger/blob/master/ObjC/PonyDebugger/PDNetworkDomainController.m
Hope that helps to other people looking something similar.
Problem
I need to intercept the networking call that the client will make in the application while the call will successfully complete. I found out that a solution is implementing the NSURLProtocol abstract class and register to the application. This seemed to solve my problems but then I hit a wall, request timeout. I use AFNetworking latest 1.x version because I need to support iOS 5.0. So now I need to find either how to extend the timeout of the initial request or another more appropriate solution to my problem. Here is what I've done. The NSURLProtocol implementation: ``` @implementation SLKURLProtocol + (NSURLRequest*) canonicalRequestForRequest:(NSURLRequest *)request { return request; } + (BOOL) canInitWithRequest:(NSURLRequest *)request { return [[[request URL] scheme] isEqualToString:@"http"]; } - (void) startLoading { dispatch_async(kBgQueue, ^{ NSLog(@"Intercept Request startLoading"); NSURLRequest* request = self.request; NSLog(@"URL: %@", request.URL); NSLog(@"HTTP Method: %@", request.HTTPMethod); NSLog(@"Data: %@", [NSString stringWithUTF8String:[request.HTTPBody bytes]]); }); } - (void) stopLoading { dispatch_async(kBgQueue, ^{ NSLog(@"Intercept Request stopLoading"); }); } @end ``` The networking ViewController: ``` @implementation SLKViewController - (void) viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [NSURLProtocol registerClass:[SLKURLProtocol class]]; [self initRequest]; } - (void) initRequest { NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://requestb.in/19kcum21"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f]; [urlRequest setHTTPMethod:@"POST"]; NSMutableData* postData = [NSMutableData data]; [postData appendData:[@"REQUEST AFNETWORKING" dataUsingEncoding:NSUTF8StringEncoding]]; [urlRequest setHTTPBody:postData]; AFHTTPRequestOperation* operation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest]; [operation setCompletionBlockWithSuccess: ^(AFHTTPRequestOperation *operation, id responseObject) { NSString* serverResponse = [operation responseString]; NSLog(@"AFNetworking Response: %@", serverResponse); } failure: ^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"AFNetworking Error: %@", error.description); }]; [operation start]; } @end ``` Here is the log output: 2014-01-15 17:07:19.518 InterceptionHttp[510:1303] Intercept Request startLoading 2014-01-15 17:07:19.520 InterceptionHttp[510:1303] URL: http://requestb.in/19kcum21 2014-01-15 17:07:19.521 InterceptionHttp[510:1303] HTTP Method: POST 2014-01-15 17:07:19.522 InterceptionHttp[510:1303] Data: REQUEST AFNETWORKING 2014-01-15 17:07:29.522 InterceptionHttp[510:400b] Intercept Request stopLoading 2014-01-15 17:07:29.522 InterceptionHttp[510:70b] AFNetworking Error: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x8a5cb10 {NSErrorFailingURLStringKey=http://requestb.in/19kcum21, NSErrorFailingURLKey=http://requestb.in/19kcum21, NSLocalizedDescription=The request timed out., NSUnderlyingError=0x8a5c220 "The request timed out."} EDIT: After jqyao answer and reading the article I have learned some topics about subclassing NSURLProtocol. But unfortunately I still can't intercept the HTTP, meaning that I want the original request to normally continue. Meanwhile, if I add the 3 lines invoking the URLProtocol delegate selectors, original request will continue but with a response of the data and not the server's. Here is what I added in `startLoading` method. ``` [[self client] URLProtocol:self didReceiveResponse:[[NSURLResponse alloc] init] cacheStoragePolicy:NSURLCacheStorageNotAllowed]; [[self client] URLProtocol:self didLoadData:data]; [[self client] URLProtocolDidFinishLoading:self]; ``` Here is my output log. 2014-02-04 17:27:22.253 InterceptionHttp[419:3a03] Intercept Request startLoading 2014-02-04 17:27:22.254 InterceptionHttp[419:3a03] URL: http://requestb.in/tsvc14ts 2014-02-04 17:27:22.255 InterceptionHttp[419:3a03] HTTP Method: POST 2014-02-04 17:27:22.255 InterceptionHttp[419:3a03] Data: { 'message': 'JSON_MESSAGE' } 2014-02-04 17:27:22.258 InterceptionHttp[419:70b] AFNetworking Response: { 'message': 'JSON_MESSAGE' } 2014-02-04 17:27:22.260 InterceptionHttp[419:1303] Intercept Request stopLoading