Get UIWebView response header

白昼怎懂夜的黑 提交于 2019-11-30 14:01:51
MagicFlow

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

This should do it for you.

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
    NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
}

Swift 4

func webViewDidFinishLoad(_ webView: UIWebView) {

    let headers = webView.request?.allHTTPHeaderFields
    for (key,value) in headers! {
        print("key \(key) value \(value)")
    }
}
toby-ying
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;

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