NSCachedURLResponse returns an object, but the UIWebView does not interpret the content

I am sending a request to UIWebView. The loaded webpage has AJAX calls. I need to analyze AJAX traffic to determine if the user is logged in or not. To do this, I installed NSURLCache in AppDelegate:

MYURLCache *cache = [[MYURLCache alloc] init]; [NSURLCache setSharedURLCache:cache]; 

This MYURLCache class correctly receives traffic that passes through the webview. I can only complete AJAX calls. Then I create my own AJAX call request. This returns a perfect response from the web server. Thus, the last step to be taken is to create an NSCachedURLResponse return object. I did it too, but the webview just doesn't return anything when returning objects. If I return only zero, WebView will load everything fine (nil is a message to NSURLCache that nothing is cached, so the webview should start loading it on its own).

 - (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request { if ([[[request URL] absoluteString] rangeOfString:@"/ajax/"].location == NSNotFound) { return nil; } else { ASIHTTPRequest *asirequest = [ASIHTTPRequest requestWithURL:[request URL]]; [asirequest setValidatesSecureCertificate:NO]; [asirequest startSynchronous]; NSError *error = [asirequest error]; NSData* data = [[asirequest responseString] dataUsingEncoding:NSUTF8StringEncoding]; // Create the cacheable response NSURLResponse *urlresponse = [[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"application/json" expectedContentLength:[data length] textEncodingName:@"UTF-8"]; NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:urlresponse data:data]; NSLog(@"cachedResponse %@", cachedResponse); NSLog(@"cachedResponse data %@", [[NSString alloc] initWithData:[cachedResponse data] encoding:NSUTF8StringEncoding]); return cachedResponse; } return nil; } 
+8
ios objective-c uiwebview nsurlcache
source share
1 answer

I found one solution to this problem ... I think this has something to do with the missing headers.

If I replaced

 NSURLResponse *urlresponse = [[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"application/json" expectedContentLength:[data length] textEncodingName:@"UTF-8"]; NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:urlresponse data:data]; 

FROM

 NSHTTPURLResponse *urlresponse = [[NSHTTPURLResponse alloc] initWithURL:request.URL statusCode:200 HTTPVersion:@"1.1" headerFields:nil]; NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:urlresponse data:data]; 

Everything works. This answer further suggested an additional custom request header. stack overflow

 NSDictionary *headers = @{@"Access-Control-Allow-Origin" : @"*", @"Access-Control-Allow-Headers" : @"Content-Type"}; NSHTTPURLResponse *urlresponse = [[NSHTTPURLResponse alloc] initWithURL:request.URL statusCode:200 HTTPVersion:@"1.1" headerFields:headers]; 

In my case, I did not need this.

+8
source share

All Articles