Get UIWebView response header












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.










share|improve this question




















  • 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
















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.










share|improve this question




















  • 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














7












7








7


3






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.










share|improve this question
















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.







ios objective-c uiwebview header






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 12:09









Community

11




11










asked Mar 11 '14 at 12:30









rawatmrawatm

1621316




1621316








  • 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














  • 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








1




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





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 Answers
5






active

oldest

votes


















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







share|improve this answer





















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





share|improve this answer



















  • 6





    sometimes it return nil?

    – neobie
    Feb 24 '16 at 13:27






  • 6





    what if the cached response is null ? how do you handle that situation ?

    – lifemoveson
    Mar 24 '16 at 4:27













  • It can be nil. Make sure to set NSURLRequestUseProtocolCachePolicy when creating your request.

    – Rivera
    Aug 8 '18 at 17:53



















0














Swift 4



func webViewDidFinishLoad(_ webView: UIWebView) {

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





share|improve this answer































    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;






    share|improve this answer

































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





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22325818%2fget-uiwebview-response-header%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









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







        share|improve this answer





















        • 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
















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







        share|improve this answer





















        • 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














        14












        14








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







        share|improve this answer















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








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 30 '15 at 20:08









        skypjack

        34.2k1357135




        34.2k1357135










        answered Dec 30 '15 at 19:47









        MagicFlowMagicFlow

        350214




        350214








        • 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














        • 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








        5




        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





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





        share|improve this answer



















        • 6





          sometimes it return nil?

          – neobie
          Feb 24 '16 at 13:27






        • 6





          what if the cached response is null ? how do you handle that situation ?

          – lifemoveson
          Mar 24 '16 at 4:27













        • It can be nil. Make sure to set NSURLRequestUseProtocolCachePolicy when creating your request.

          – Rivera
          Aug 8 '18 at 17:53
















        11














        This should do it for you.



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





        share|improve this answer



















        • 6





          sometimes it return nil?

          – neobie
          Feb 24 '16 at 13:27






        • 6





          what if the cached response is null ? how do you handle that situation ?

          – lifemoveson
          Mar 24 '16 at 4:27













        • It can be nil. Make sure to set NSURLRequestUseProtocolCachePolicy when creating your request.

          – Rivera
          Aug 8 '18 at 17:53














        11












        11








        11







        This should do it for you.



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





        share|improve this answer













        This should do it for you.



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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 5 '14 at 15:22









        DBDDBD

        14.4k125280




        14.4k125280








        • 6





          sometimes it return nil?

          – neobie
          Feb 24 '16 at 13:27






        • 6





          what if the cached response is null ? how do you handle that situation ?

          – lifemoveson
          Mar 24 '16 at 4:27













        • It can be nil. Make sure to set NSURLRequestUseProtocolCachePolicy when creating your request.

          – Rivera
          Aug 8 '18 at 17:53














        • 6





          sometimes it return nil?

          – neobie
          Feb 24 '16 at 13:27






        • 6





          what if the cached response is null ? how do you handle that situation ?

          – lifemoveson
          Mar 24 '16 at 4:27













        • It can be nil. Make sure to set NSURLRequestUseProtocolCachePolicy when creating your request.

          – Rivera
          Aug 8 '18 at 17:53








        6




        6





        sometimes it return nil?

        – neobie
        Feb 24 '16 at 13:27





        sometimes it return nil?

        – neobie
        Feb 24 '16 at 13:27




        6




        6





        what if the cached response is null ? how do you handle that situation ?

        – lifemoveson
        Mar 24 '16 at 4:27







        what if the cached response is null ? how do you handle that situation ?

        – lifemoveson
        Mar 24 '16 at 4:27















        It can be nil. Make sure to set NSURLRequestUseProtocolCachePolicy when creating your request.

        – Rivera
        Aug 8 '18 at 17:53





        It can be nil. Make sure to set NSURLRequestUseProtocolCachePolicy when creating your request.

        – Rivera
        Aug 8 '18 at 17:53











        0














        Swift 4



        func webViewDidFinishLoad(_ webView: UIWebView) {

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





        share|improve this answer




























          0














          Swift 4



          func webViewDidFinishLoad(_ webView: UIWebView) {

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





          share|improve this answer


























            0












            0








            0







            Swift 4



            func webViewDidFinishLoad(_ webView: UIWebView) {

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





            share|improve this answer













            Swift 4



            func webViewDidFinishLoad(_ webView: UIWebView) {

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






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 10:01









            Charlie SeligmanCharlie Seligman

            2,33723262




            2,33723262























                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;






                share|improve this answer






























                  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;






                  share|improve this answer




























                    0












                    0








                    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;






                    share|improve this answer















                    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;







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 29 '18 at 8:56









                    dkb

                    1,43411625




                    1,43411625










                    answered Dec 29 '18 at 5:22









                    toby-yingtoby-ying

                    1




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





                        share|improve this answer




























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





                          share|improve this answer


























                            -1












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





                            share|improve this answer













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






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 18 '18 at 16:45









                            AnthonyRAnthonyR

                            958626




                            958626






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22325818%2fget-uiwebview-response-header%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                MongoDB - Not Authorized To Execute Command

                                in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                                Npm cannot find a required file even through it is in the searched directory