Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect redirect in a UIWebView

Tags:

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.

like image 559
zorro2b Avatar asked May 26 '11 20:05

zorro2b


2 Answers

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

like image 121
thelaws Avatar answered Oct 15 '22 20:10

thelaws


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    } } 
like image 29
PowerAktar Avatar answered Oct 15 '22 21:10

PowerAktar