Get UIWebView response header

后端 未结 5 1184
广开言路
广开言路 2021-01-05 07:57

I have looked into ways to get response header from UIWebview response. This SO question discusses it. But I am unsure if this is allowed by apple. I will have a webview sho

相关标签:
5条回答
  • 2021-01-05 08:03

    This should do it for you.

    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
        NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
    }
    
    0 讨论(0)
  • 2021-01-05 08:05

    Swift 4

    func webViewDidFinishLoad(_ webView: UIWebView) {
    
        let headers = webView.request?.allHTTPHeaderFields
        for (key,value) in headers! {
            print("key \(key) value \(value)")
        }
    }
    
    0 讨论(0)
  • 2021-01-05 08:13

    Swift 4

    override func viewDidLoad() {
        super.viewDidLoad()
        webView.delegate = self
    }
    
    func webViewDidFinishLoad(_ webView: UIWebView) {
        if let request = webView.request {
            let response = URLCache.shared.cachedResponse(for: request)
            // ...
        }
    }
    
    0 讨论(0)
  • 2021-01-05 08:22

    In addition to the answer provided by DBD, you will need to ensure that

    1. The containing UIViewController is marked as a UIWebViewDelegate in the .h file:

      @interface VIMAuthenticationViewController : UIViewController <UIWebViewDelegate>
      
    2. The UIWebView's delegate is set to the containing UIViewController. This can be done directly in the Interface Building or by linking the web view and adding the following in view did load in .m fie:

      [self.WebView setDelegate:self];
      
    3. Add the code as provided by DBD:

      (void)webViewDidFinishLoad:(UIWebView *)webView {
         NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
         NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
      }
      
    0 讨论(0)
  • 2021-01-05 08:25
    NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];   
    NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
    

    this function sometimes it returns nil, I looked it up, If the file size exceeds 50kb, NSURLConnection does not call storeCachedResponse: forRequest;

    0 讨论(0)
提交回复
热议问题