【问题标题】:Getting img tag response headers from UIWebView从 UIWebView 获取 img 标签响应头
【发布时间】:2016-11-16 10:09:46
【问题描述】:

我有一个带有一些静态 HTML 的 UIWebView。 我想从该 html 中获取 img 的响应标头。

代码:

let html = "<html><body><img src=\"\(imageUrl)\" alt=\"test\" width=\"70\" height=\"70\"></body></html>"
    webView.loadHTMLString(html, baseURL: nil)

extension ViewController: UIWebViewDelegate {
func webViewDidFinishLoad(_ webView: UIWebView) {
    if let html = webView.stringByEvaluatingJavaScript(from: "document.body.innerHTML") {
        print("html: \(html)")

    }

    print("finish load")

    let res = URLCache.shared.cachedResponse(for: webView.request!)
    print("res: \(res)")
    print("headers: \(res?.response)")
}
}

缓存的响应总是 nil。

有没有办法获取标题?

谢谢

【问题讨论】:

    标签: ios swift uiwebview


    【解决方案1】:

    建议使用 NSURLConnection 发出请求并使用 NSURLResponse 提取 headers 字段。可以关注阅读this

    【讨论】:

    • 这对我不起作用。我正在使用外部服务,为了让它工作,我需要从 html 调用这个 url。
    • 你以前试过这个 - stackoverflow.com/questions/22325818/… 吗?
    • 是的,看看我的问题中的代码......正是这样。
    • 你的 HTML 内容有什么吗?
    • 是的,我得到了我加载的 html
    猜你喜欢
    • 2017-07-28
    • 2015-06-08
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多