【问题标题】:NSCachedURLResponse returns object, but UIWebView does not interprets contentNSCachedURLResponse 返回对象,但 UIWebView 不解释内容
【发布时间】:2013-02-21 00:59:24
【问题描述】:

我正在向 UIWebView 发送请求。加载的网页上有 AJAX 调用。我需要分析 AJAX 流量以确定用户是否登录。为此,我在 AppDelegate 中安装了一个 NSURLCache:

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

这个 MYURLCache 类正确接收通过 webview 运行的流量。我只能终止 AJAX 调用。然后我产生一个自己的 AJAX 调用请求。这个返回来自网络服务器的完美响应。所以现在要做的最后一步是构造 NSCachedURLResponse 返回对象。我也管理了这个,但是 webview 在返回对象时根本不做任何事情。如果我只返回 nil,WebView 加载一切正常(nil 是 NSURLCache 的消息,没有缓存,所以 webview 应该开始自己加载它)。

- (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;
}

【问题讨论】:

    标签: ios objective-c uiwebview nsurlcache


    【解决方案1】:

    我找到了解决这个问题的方法...我认为这与缺少的标题有关。

    如果我替换

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

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

    整个事情都有效。该答案还建议了请求的附加自定义标头。 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];
    

    就我而言,我不需要它。

    【讨论】:

    • 我希望 uiwebview 离线工作。所以我使用了stackoverflow.com/a/17149902/935381 并将响应存储在文档目录中。但是在离线模式下,当我尝试使用 NSData * sData = (NSData *)[NSData dataWithContentsOfFile:filePath]; [webview loadData:sData MIMEType:@"text/html" textEncodingName:nil baseURL:nil];但它没有显示正确的内容。它显示编码数据...
    猜你喜欢
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多