NSURLProtocol subclass changes URLs automatically

I am using UIWebView and have implemented own subclass for NSURLProtocol to intercept PDF File from the response. Everything was working fine. but, I realized for some of our website where Re-writing proxy is implemented. URL is getting change & that causes HTTP 404.

To understand the cases I tried to return NO in canInitWithRequest. At that time it worked perfectly but as I send YES there its subsequent requests URL gets changed.

This is what I have written in startLoading Method

- (void)startLoading {

    NSMutableURLRequest* request = self.request.mutableCopy;
    NSLog(@"%@",request.URL);
    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];

    NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        if([response isKindOfClass:[NSHTTPURLResponse class]]){
            NSDictionary *aInfo = [(NSHTTPURLResponse*)response allHeaderFields];
            if([(NSString*)aInfo[@"Content-Type"] containsString:@"application/pdf"]){

                // Code to save DATA as PDF File 
                //in Document Directory via call back to other classes.

                [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowedInMemoryOnly];
                [self.client URLProtocol:self didLoadData:nil];
                [self.client URLProtocolDidFinishLoading:self];
            }else{
                [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
                [self.client URLProtocol:self didLoadData:data];
                [self.client URLProtocolDidFinishLoading:self];
            }
            aInfo = nil;
        }

    }];
    [postDataTask resume];
}
链接地址: http://www.djcxy.com/p/34810.html

上一篇: 加载本地HTML页面

下一篇: NSURLProtocol子类会自动更改URL