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:
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.
A little late now but I would use the webViewDidFinishLoad and the webViewDidStartLoad delegate methods to detect redirects as follows:
The best you can really do is observe the
webView:shouldStartLoadWithRequest:navigationType:
delegate method. I assume that redirects fall underUIWebViewNavigationTypeOther
.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.