How to detect redirect in a UIWebView

42,077

Solution 1

The best you can really do is observe the webView:shouldStartLoadWithRequest:navigationType: delegate method. I assume that redirects fall under UIWebViewNavigationTypeOther.

Solution 2

A little late now but I would use the webViewDidFinishLoad and the webViewDidStartLoad delegate methods to detect redirects as follows:

- (void)webViewDidStartLoad:(UIWebView *)webView{

    myRequestedUrl= [webView.request mainDocumentURL];
    NSLog(@"Requested url: %@", myRequestedUrl);    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{  

    myLoadedUrl = [webView.request mainDocumentURL];
    NSLog(@"Loaded url: %@", myLoadedUrl);

   //psudocode
   if(myRequestedUrl is not the same as myLoadedUrl){
      doSomething
   }
}

Solution 3

I am working through this same issue. Originally I followed the advice here and used maindocumenturl for manually inputting urls into a history list for back/forward navigation. However, this didn't give very accurate urls, whether or not you got the url from didstartload or didfinishload. If you want to feel my pain, try navigate through a google search and you will see what I am talking about, maindocumenturl is completely useless in that environment. By contrast, the absolute url property used with webviewdidfinishload works much better, and actually lets the user create a usable history. That's my two cents.

Share:
42,077
zorro2b
Author by

zorro2b

Updated on July 09, 2022

Comments

  • zorro2b
    zorro2b almost 2 years

    I am using a UIWebView as an embedded browser within my app. The problem I have is tracking the URL that should be displayed in the URL bar.

    When doing a Google search, the results page often generates links like this:

    http://www.google.com/url?sa=t&source=web&cd=1&ved=0CC4QFjAA&url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FDog&rct=j&q=dogs&ei=27XeTc6RFIWEvAO858DHBQ&usg=AFQjCNGkEDcWzea4pSlurHhcuQfqFcp_pw

    When the user clicks this link, the UIWebView first reports this link and then the redirected link in shouldStartLoadWithRequest:navigationType:.

    How can I tell this is a redirect as opposed to some supplementary site being loaded for images or other elements on the page? As it stands my URL bar is displaying the long link from Google in the above example rather than updating to the Wikipedia URL.

  • Admin
    Admin about 12 years
    UIWebViewNavigationType only indicates the user's action in initiating the request, it doesn't give any details about what the server is doing.
  • Admin
    Admin about 12 years
    Have you tried this? I've found the request property of webView given by webViewDidStartLoad: to be unreliable. It's best to store the original request used to load the UIWebView and compare that against the request provided by webViewDidFinishLoad:.
  • PowerAktar
    PowerAktar about 12 years
    Yes thats also a good idea but I was under the impression request in webViewDidStartLoad just gives the input request. I have tested it though and it does work, though maybe in complex situations it might cough up.
  • Admin
    Admin about 12 years
    I've found that webViewDidStartLoad returns an NSMutableURLRequest that is empty, and thus can't be used for much. Not doing anything explicitly complex, just loading websites using loadRequest:, the site loads fine and webViewDidFinishLoad returns the request correctly otherwise.