I use the NSURLProtocol subclass to intercept all HTTP calls and change the user agent, as well as add other HTTP headers that my server needs.
-(id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id <NSURLProtocolClient>)client { NSMutableURLRequest* lInnerRequest; //************************************************ lInnerRequest = [request mutableCopy]; [lInnerRequest setValue:@"MyUserAgent" forHTTPHeaderField:@"User-Agent"]; //************************************************ self = [super initWithRequest:lInnerRequest cachedResponse:cachedResponse client:client]; //************************************************ if (self) { self.innerRequest = lInnerRequest; } //***********************************00************* [lInnerRequest release]; //************************************************ return self; }
My protocol uses NSURLConnection
- (void)startLoading { self.URLConnection = [NSURLConnection connectionWithRequest:self.innerRequest delegate:self]; }
Then I implement the entire delegate method in NSURLConnection, forwarding the call to the equivalent NSURLProtocolClient method. This works well, but when I upload data to the server, my code that uses NSURLConnection does not return:
connection: didSendBodyData: totalBytesWritten: totalBytesExpectedToWrite:
I understand why this has been happening since I did not implement this method in NSURLProtocol, since there is no equivalent NSURLProtocolClient method that can be used to report progress.
Has anyone found any workaround for this?
source share