7

I have looked into ways to get response header from UIWebview response. This SO question discusses it. But I am unsure if this is allowed by apple. I will have a webview showing a loaded login page and need to get the response headers after a successful login. Also this

does something to get status code. But it create a duplicate NSUrlConnection request. Is there any way by which I can achieve this? I would appreciate any information on this.

Community
  • 1
  • 1
Madhur Rawat
  • 468
  • 1
  • 7
  • 18
  • 1
    As the creator of that answer, I personally can't say if it would pass inspection, although I have used similar techniques in apps that I have put on the store which have passed just fine. YMMV, the only way to know for sure is to submit the app for review. – Richard J. Ross III Mar 11 '14 at 13:47

5 Answers5

14

In addition to the answer provided by DBD, you will need to ensure that

  1. The containing UIViewController is marked as a UIWebViewDelegate in the .h file:

    @interface VIMAuthenticationViewController : UIViewController <UIWebViewDelegate>
    
  2. The UIWebView's delegate is set to the containing UIViewController. This can be done directly in the Interface Building or by linking the web view and adding the following in view did load in .m fie:

    [self.WebView setDelegate:self];
    
  3. Add the code as provided by DBD:

    (void)webViewDidFinishLoad:(UIWebView *)webView {
       NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
       NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
    }
    
skypjack
  • 49,335
  • 19
  • 95
  • 187
MagicFlow
  • 477
  • 3
  • 17
  • 5
    Holy crap. Additional context provided in a mobile app answer. That has to be a sign of the apocalypse and worthy of more +1s than I can sadly give. – Erik Reppen Jan 22 '16 at 23:46
11

This should do it for you.

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
    NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
}
DBD
  • 23,075
  • 12
  • 60
  • 84
0

Swift 4

func webViewDidFinishLoad(_ webView: UIWebView) {

    let headers = webView.request?.allHTTPHeaderFields
    for (key,value) in headers! {
        print("key \(key) value \(value)")
    }
}
Charlie S
  • 4,366
  • 6
  • 59
  • 97
0
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];   
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);

this function sometimes it returns nil, I looked it up, If the file size exceeds 50kb, NSURLConnection does not call storeCachedResponse: forRequest;

dkb
  • 4,389
  • 4
  • 36
  • 54
breeze
  • 1
-1

Swift 4

override func viewDidLoad() {
    super.viewDidLoad()
    webView.delegate = self
}

func webViewDidFinishLoad(_ webView: UIWebView) {
    if let request = webView.request {
        let response = URLCache.shared.cachedResponse(for: request)
        // ...
    }
}
AnthonyR
  • 3,485
  • 1
  • 18
  • 41