How to detect redirect in a UIWebView
The best you can really do is observe the webView:shouldStartLoadWithRequest:navigationType:
delegate method. I assume that redirects fall under UIWebViewNavigationTypeOther
.
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 }}
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.