NSCachedURLResponse returns object, but UIWebView does not interprets content

夙愿已清 提交于 2019-12-30 17:35:20

问题


I am sending a request to a UIWebView. There are AJAX-calls on the loaded webpage. I need to analyze the AJAX-traffic in order to determinate, if the user is logged in or not. For doing this I installed a NSURLCache in the AppDelegate:

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

This MYURLCache-class correctly receives the traffic that runs through the webview. I can terminate the AJAX-calls only. I then spawn an own request of the AJAX-calls. This one returns the perfectly fine response from the webserver. So the last step to do now is constructing the NSCachedURLResponse return object. I also managed this, but the webview simply does nothing when returning the objects. If I return just nil, the WebView loads everything fine (nil is the message for the NSURLCache, that nothing is cached, so the webview should start to load 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;
}

回答1:


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

If I replace

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

With

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

The whole thing works. This answer in addition suggested the additional custom header of the request. https://stackoverflow.com/a/15234850/274518

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 needed it.



来源:https://stackoverflow.com/questions/15243913/nscachedurlresponse-returns-object-but-uiwebview-does-not-interprets-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!