Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get UIWebView response header

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.

like image 920
Madhur Rawat Avatar asked Mar 11 '14 12:03

Madhur Rawat


2 Answers

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]);
    }
    
like image 134
MagicFlow Avatar answered Oct 09 '22 21:10

MagicFlow


This should do it for you.

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
    NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
}
like image 23
DBD Avatar answered Oct 09 '22 20:10

DBD