21

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.

zorro2b
  • 2,227
  • 4
  • 28
  • 45
  • I have a tutorial on my website that shows you how to do that. Check out [Building a Web Browser with UIWebView (Part 3)](http://iosdeveloperzone.com/2011/05/22/tutorial-building-a-web-browser-with-uiwebview-part-3/). At the end of the tutorial there is downloadable source code that you are free to use in your own project. Let me know if it does what you want. If not maybe I can suggest how you might modify it. – idz May 26 '11 at 21:46
  • Thanks for that. I still can't definitively detect a redirect, but using the [request mainDocumentURL] I think will give me what I need. – zorro2b Jun 01 '11 at 09:42
  • @idz You saved my backside. Thanks. – Jezen Thomas Mar 19 '13 at 14:06
  • Read the section entitled "Keeping the Address Bar in Sync". – idz Nov 22 '12 at 17:19
  • sorry, still available. but the question is, where in your tutorial do you detect the redirect? – drct Nov 20 '12 at 09:44
  • @drct What's not available anymore? The linked tutorial and source code is (just rechecked now) and the -[NSURLRequest mainDocumentURL] is still available (see https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.html#//apple_ref/doc/uid/20001695-BCIBDDEC) – idz Nov 16 '12 at 00:23
  • not available anymore – drct Nov 15 '12 at 16:08

3 Answers3

19

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

thelaws
  • 7,991
  • 6
  • 35
  • 54
  • `UIWebViewNavigationType` only indicates the user's action in initiating the request, it doesn't give any details about what the server is doing. –  Apr 25 '12 at 10:22
7

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
   }
}
PowerAktar
  • 2,341
  • 1
  • 21
  • 17
  • 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:`. –  Apr 26 '12 at 15:33
  • 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. – PowerAktar Apr 26 '12 at 20:07
  • 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. –  Apr 27 '12 at 08:51
1

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.

user2104778
  • 992
  • 1
  • 14
  • 38