Get HTTP Response header on UIWebView

后端 未结 2 1315
你的背包
你的背包 2020-12-17 07:28

How can I get HTTP headers response from WebView? I\'ve found semi-solutions at Stackoverflow, but it\'s written on Objective-C and can\'t convert it to Swift (I\'ve tried w

相关标签:
2条回答
  • 2020-12-17 07:41

    Just using this in SWIFT 3.1 :

    if let request = webView.request {
                if let resp = URLCache.shared.cachedResponse(for: request) {
                    if let response = resp.response as? HTTPURLResponse {
                        print(response.allHeaderFields)
                    }
                }
            }
    
    0 讨论(0)
  • 2020-12-17 07:47

    Swift

    Swift is more stringent ; you want to protect yourself against nil pointers and optionals:

    • check that the webView actually has a request
    • check that the request actually has a NSCachedURLResponse
    • type-check the response against NSHTTPURLResponse
    func webViewDidFinishLoad(webView: UIWebView) {
        if let request = webView.request {
            if let resp = NSURLCache.sharedURLCache().cachedResponseForRequest(request) {
                if let response = resp.response as? NSHTTPURLResponse {
                    print(response.allHeaderFields)
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题