Get HTTP Response header on UIWebView

南笙酒味 提交于 2019-11-29 05:19:05

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

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