如何从WebView获取HTTP标头响应?我找到了半解决方案at Stackoverflow,但它是用Objective-C编写的,无法将其转换为Swift(我用我糟糕的Obj-C知识进行了尝试) )。
Objective-C代码:
var table = document.getElementById(f);
var row = table.insertRow();
var cell1 = row.insertCell();
var cell2 = row.insertCell();
cell1.innerHTML = text1;
cell2.innerHTML = text2;
答案 0 :(得分:3)
<强>夫特强>
斯威夫特更严格;你想保护自己不受nil
指针和optionals
:的影响
webView
是否确实有request
request
是否确实有NSCachedURLResponse
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)
}
}
}
}
答案 1 :(得分:0)
只需在 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)
}
}
}