How to detect redirect in a UIWebView

丶灬走出姿态 提交于 2019-11-29 05:31:37

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!