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 with my poor Obj-C knowledge).
Objective-C code:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
}
Swift
Swift is more stringent ; you want to protect yourself against nil
pointers and optionals
:
webView
actually has a request
request
actually has a 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)
}
}
}
}