Search code examples
ioshttp-headersrubymotion

How to Constantly monitor response headers with UIWebview for iOS App


I have a UIWebview in my iOS app that needs to monitor the response headers for each page it goes to, I have tried to look for it on

webViewDidFinishLoad

and

shouldStartLoadWithRequest

but have not gotten far, further research has lead me to

didReceiveResponse

for NSURLConnection, but my understanding is that this can only get the first response, and can not get the second, or further responses.

If it would help any, I'm using Rubymotion to implement this.


Solution

  • It's not possible, I'm afraid. You can get the current web URL using JavaScript:

    https://stackoverflow.com/a/2494051/204044

    (or anything else that JS can access, for that matter)...but you're not able to get the headers. This is a security thing.

    If you have control over the web server, you could pass in information that you need in a javascript object like this:

    // In your webserver's code, assuming it's PHP:
    <script type="text/javascript">
      window.customDataString = "<?= $custom_data ?>";
    </script>
    
    // In Objective-C
    NSString *customDataString = [webView stringByEvaluatingJavaScriptFromString:@"window.customDataString"];