如何从WebView获得HTTP标头响应?我找到了半解决方案,但是它是用Objective- C编写的,不能将其转换为Swift(我已经尝试过使用较差的Obj-C知识了)。
Objective-C代码:
- (void)webViewDidFinishLoad:(UIWebView *)webView { NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request]; NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]); }
迅速
斯威夫特更严格; 您想保护自己免受nil指针和optionals:
nil
optionals
webView
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) } } } }