Android Webview, How to Handle Redirects in App Instead of Opening a Browser

Android WebView, how to handle redirects in app instead of opening a browser

Create a WebViewClient, and override the shouldOverrideUrlLoading method.

webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
view.loadUrl(url);
return false; // then it is not handled by default action
}
});

Android, catch webview redirection url

You could use a webClient and implement shouldOverrideUrlLoading to intercept all the urls
before the WebView loads them.

    mWebView.setWebViewClient(new WebViewClient() {


@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Here put your code
Log.d("My Webview", url);

// return true; //Indicates WebView to NOT load the url;
return false; //Allow WebView to load url
}
});

Android, How to retrieve redirect URL from a WebView?

Finally thanks to the answer given to my question by Udi I, with a little change, i managed to find a solution to the problem. Here is the code which worked for me:

    webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView wView, String url) {
return (url.indexOf("some part of my redirect uri") > -1);
}
});
webView.loadUrl(myUrl); //myUrl is the initial url.

Using the above code, if there will be any url containing redirect uri, webView won't load it. Else, webView will load it. Also thanks to Kamil Kaminski.



Related Topics



Leave a reply



Submit