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.

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]);
    }
    

Get HTTP Response header on UIWebView, Swift. Swift is more stringent ; you want to protect yourself against nil pointers and optionals : check that the webView actually has a request  UIWebView Get Response Headers. UIWebView is can view Web pages easily, but does not have the ability to control an iOS side by checking the HTTP status. you can has become a double request and reread again in loadRequest, if you would use the loadData, the browser back function / You will also be available on Forward.

This should do it for you.

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
    NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
}

Response headers / statusCode from a UIWebView (using , Response headers / statusCode from a UIWebView (using workaround till the webview gets extended!) WebviewResponseHeaders.mm. The headers read-only property of the Response interface contains the Headers object associated with the response. Syntax var myHeaders = response.headers; Value. A Headers object. Example. In our Fetch Response example (see Fetch Response live) we create a new Request object using the Request() constructor, passing it a JPG path

Swift 4

func webViewDidFinishLoad(_ webView: UIWebView) {

    let headers = webView.request?.allHTTPHeaderFields
    for (key,value) in headers! {
        print("key \(key) value \(value)")
    }
}

Swift get headers from UIWebView response - uiwebview - html, I am unable to get headers from a UIWebView response as the response apparently hasn't been cached. Is there a workaround? I have tried code from here. If there are multiple response headers with the same name, then their values are returned as a single concatenated string, where each value is separated from the previous one by a pair of comma and space. The getResponseHeader () method returns the value as a UTF byte sequence.

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;

iphone - UIWebView capturing the response headers, ios - How to get response headers of a request URL from UIWebView Dele ios objective-c uiwebview uiwebviewdelegate response-headers. The get() method of the Headers interface returns a byte string of all the values of a header within a Headers object with a given name. If the requested header doesn't exist in the Headers object, it returns null.

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)
        // ...
    }
}

UIWebView: Get response headers, The web server send headers like "BasketAmount" and so on. I need it! Is there any way to get all response (not request) headers in xamarin.ios? There is no way to get the response headers from a WebView on Android. There however a few workarounds that can be implemented in Tabris.js. Use fetch() to get the page and than but the html in the webview. Use embedded javascript inside the webview to make the request to the target page and intercept the headers that way.

Get HTTP Response header on UIWebView, 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  Example. This example examines the headers in the request's readystatechange event handler, XMLHttpRequest.onreadystatechange.The code shows how to obtain the raw header string, as well as how to convert it into an array of individual headers and then how to take that array and create a mapping of header names to their values.

Changing the headers for UIWebKit HTTP requests, It relies on the public API for setting HTTP header fields. When you load a web page from the internet through UIWebView, you can provide a of the delegate, you'll even get an NSURLRequest object you can look at, but  Inspecting Response Headers in the Android WebView The Android WebView is great for presenting users with web content in native or hybrid applications. Its ability to bind JavaScript code to a client-side interface, navigation controls, and relatively small API surface are all great.

UIWebView, Use the stopLoading() method to stop loading, and the isLoading property to find out if a web view is in the process of loading. If you allow the user to move back  For security reasons, some headers can only be controller by the user agent. These headers include the forbidden header names and forbidden response header names. Syntax myHeaders.getAll(name); Parameters name The name of the HTTP header whose values you want to retrieve from the Headers object.

Comments
  • 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.
  • 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.
  • sometimes it return nil?
  • what if the cached response is null ? how do you handle that situation ?
  • It can be nil. Make sure to set NSURLRequestUseProtocolCachePolicy when creating your request.