<legend id='fcTiu'><style id='fcTiu'><dir id='fcTiu'><q id='fcTiu'></q></dir></style></legend>
      <tfoot id='fcTiu'></tfoot>
      • <bdo id='fcTiu'></bdo><ul id='fcTiu'></ul>
      <i id='fcTiu'><tr id='fcTiu'><dt id='fcTiu'><q id='fcTiu'><span id='fcTiu'><b id='fcTiu'><form id='fcTiu'><ins id='fcTiu'></ins><ul id='fcTiu'></ul><sub id='fcTiu'></sub></form><legend id='fcTiu'></legend><bdo id='fcTiu'><pre id='fcTiu'><center id='fcTiu'></center></pre></bdo></b><th id='fcTiu'></th></span></q></dt></tr></i><div id='fcTiu'><tfoot id='fcTiu'></tfoot><dl id='fcTiu'><fieldset id='fcTiu'></fieldset></dl></div>

      1. <small id='fcTiu'></small><noframes id='fcTiu'>

      2. NSCachedURLResponse 返回对象,但 UIWebView 不解释内容

        NSCachedURLResponse returns object, but UIWebView does not interprets content(NSCachedURLResponse 返回对象,但 UIWebView 不解释内容)

            1. <i id='COmKc'><tr id='COmKc'><dt id='COmKc'><q id='COmKc'><span id='COmKc'><b id='COmKc'><form id='COmKc'><ins id='COmKc'></ins><ul id='COmKc'></ul><sub id='COmKc'></sub></form><legend id='COmKc'></legend><bdo id='COmKc'><pre id='COmKc'><center id='COmKc'></center></pre></bdo></b><th id='COmKc'></th></span></q></dt></tr></i><div id='COmKc'><tfoot id='COmKc'></tfoot><dl id='COmKc'><fieldset id='COmKc'></fieldset></dl></div>
                <bdo id='COmKc'></bdo><ul id='COmKc'></ul>

              • <small id='COmKc'></small><noframes id='COmKc'>

              • <tfoot id='COmKc'></tfoot>
                  <tbody id='COmKc'></tbody>
              • <legend id='COmKc'><style id='COmKc'><dir id='COmKc'><q id='COmKc'></q></dir></style></legend>

                1. 本文介绍了NSCachedURLResponse 返回对象,但 UIWebView 不解释内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  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];
                  

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

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

                  推荐答案

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

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

                  如果我替换

                  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

                  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.

                  这篇关于NSCachedURLResponse 返回对象,但 UIWebView 不解释内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  iOS UIWebView app opens link in Safari(iOS UIWebView 应用在 Safari 中打开链接)
                  Close UIWebView using javascript:window.close();(使用 javascript:window.close() 关闭 UIWebView;)
                  Trouble hiding iAd banner and displaying UIWebView in its place(无法隐藏 iAd 横幅并在其位置显示 UIWebView)
                  Knowing when AJAX has loaded in UIWebView(知道何时在 UIWebView 中加载了 AJAX)
                  Load image/logo in existing HTML input field - UIWebView(在现有的 HTML 输入字段中加载图像/徽标 - UIWebView)
                  (lots of) UIWebView memory leaks((很多)UIWebView 内存泄漏)

                2. <i id='zNJQx'><tr id='zNJQx'><dt id='zNJQx'><q id='zNJQx'><span id='zNJQx'><b id='zNJQx'><form id='zNJQx'><ins id='zNJQx'></ins><ul id='zNJQx'></ul><sub id='zNJQx'></sub></form><legend id='zNJQx'></legend><bdo id='zNJQx'><pre id='zNJQx'><center id='zNJQx'></center></pre></bdo></b><th id='zNJQx'></th></span></q></dt></tr></i><div id='zNJQx'><tfoot id='zNJQx'></tfoot><dl id='zNJQx'><fieldset id='zNJQx'></fieldset></dl></div>
                    <bdo id='zNJQx'></bdo><ul id='zNJQx'></ul>
                      <tbody id='zNJQx'></tbody>

                      <small id='zNJQx'></small><noframes id='zNJQx'>

                          <legend id='zNJQx'><style id='zNJQx'><dir id='zNJQx'><q id='zNJQx'></q></dir></style></legend>
                          <tfoot id='zNJQx'></tfoot>