Well, I dropped the idea of custom NSURLProtocol. I wrote a class which confirms to NSURLConnectionDelegate, NSURLConnectionDataDelegate etc as per your need. This class serves as a common delegate to all NSURLConnection instances. It has a property of type id(depend upon requirements), this property holds the object which is creating NSURLConnection and interested in delegate callbacks as well.
URLRequestLogger instance serves as a common delegate to all NSURLConnection instances.
Each NSURLConnection has a URLRequestLogger instance as a delegate.
#import <Foundation/Foundation.h>
@interface URLRequestLogger : NSObject<NSURLConnectionDataDelegate>
-(id)initWithConnectionDelegate:(id<NSURLConnectionDataDelegate>)connectionDelegate;
@end
implementation file
#import "URLRequestLogger.h"
@interface URLRequestLogger ()
@property(nonatomic, assign) id<NSURLConnectionDataDelegate> connectionDelegate;
@end
@implementation URLRequestLogger
-(id)initWithConnectionDelegate:(id<NSURLConnectionDataDelegate>)connectionDelegate
{
self = [super init];
if (self)
{
_connectionDelegate = connectionDelegate;
}
return self;
}
//one can implement all the delegates related to NSURLConnection as per need
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//log reponse info
if ([response isKindOfClass:[NSHTTPURLResponse class]])
{
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *)response;
NSDictionary * responseHeaders = [httpResponse allHeaderFields];
NSInteger statusCode = [httpResponse statusCode];
//save as many info as we can
}
//if connectionDelegate is interested in it, inform it
if ([self.connectionDelegate respondsToSelector:@selector(connection:didReceiveResponse:)])
{
[self.connectionDelegate connection:connection didReceiveResponse:response];
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//log the error and other info
//if connectionDelegate is interested in it, inform it
if ([self.connectionDelegate respondsToSelector:@selector(connection:didFailWithError:)])
{
[self.connectionDelegate connection:connection didFailWithError:error];
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//log the connection finish info
//response time and download time etc
//if connectionDelegate is interested in it, inform it
if ([self.connectionDelegate respondsToSelector:@selector(connectionDidFinishLoading:)])
{
[self.connectionDelegate connectionDidFinishLoading:connection];
}
}
@end
MyViewController creates an NSURLConnection and is interested in delegate methods as well. At the same time we want to log all the details about request and response.
#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController<NSURLConnectionDataDelegate>
@end
implementation file
#import "MyViewController.h"
#import "URLRequestLogger.h"
@implementation MyViewController
//MyViewController class creates a request and interested in connection delegate callbacks
//MyViewController confirms to NSURLConnectionDelegate.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self sendRequest];
}
-(void)sendRequest
{
/*
connection delegate here would be our URLRequestLogger class instance which holds MyViewController instance
to return the callbacks here after logging operations are finished
*/
URLRequestLogger * requestLogger = [[URLRequestLogger alloc] initWithConnectionDelegate:self];
[NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.org"]] delegate:requestLogger];
}
#pragma mark - NSURLConnection delegate methods
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//this callback is received from URLRequestLogger after logging operation is completed
//do something. Update UI etc...
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//this callback is received from URLRequestLogger after logging operation is completed
//do something. Update UI etc...
}
@end