How to get Webview iframe link to launch the browser? - android

I'm using a WebView to display a page in which the html includes an iframe where src="xxxxx.php".
This iframe loads as an ad image with an underlying link. If I click on that image (link), it tries to load the new page within original iframe (which doesn't show much in that little space). What I want to happen is clicking on the link to open the referred page in a new browser window, leaving my app as is.
If I use the Android browser to display the original page and click on this iframe, it loads the link as a new page. How do I get the same behavior with a WebView? Using a WebViewClient with shouldOverrideUrlLoading() doesn't seem to be called by the iframe link.

I had a similar issue with google ads in a WebView source, since they load in an iframe as well. This is how I resolved it:
Try this in your WebViewClient, typically under your shouldOverrideUrlLoading()
#Override
public void onLoadResource (WebView view, String url) {
if (url.contains("googleads")) {
if(view.getHitTestResult().getType() > 0){
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
view.stopLoading();
Log.i("RESLOAD", Uri.parse(url).toString());
}
}
}

I can propose one fix to previous code:
#Override
public void onLoadResource (WebView view, String url) {
if (url.contains("googleads")) {
if(view.getHitTestResult() != null &&
(view.getHitTestResult().getType() == HitTestResult.SRC_ANCHOR_TYPE ||
view.getHitTestResult().getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE)){
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
view.stopLoading();
}
}
}

To detect links clicks inside of the iframe. The links should have additional parameters. I've found that:
shouldOverrideUrlLoading of the WebViewClient will be executed for a case when iframe link has target parameter target ="_parent" or target="_top",
onCreateWindow of the WebViewClient, in case, if the iframe link contains target="_blank" parameter
Seems the iframe links without target parameter not possible to track exactly via WebViewClient

Related

webview hyperlink opening in default web browser

I have a android app which contains jsp page.
On this jsp page one hyperlink is created and linked another jsp page which contains download pdf code.
When I click on hyperlink it will open in android default browser and download starts.
I want to download pdf inside web view without opening web browser.
For that you can use like this:
Every time you click a link WebViewClient's shouldOverrideUrlLoading method will be called. Check that url points to pdf file and do what you want. For example, you can view pdf.
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (url.endsWith(".pdf")) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
// if want to download pdf manually create AsyncTask here
// and download file
return true;
}
return false;
}
});

How to set webviewClient for webView dynamically?

For instance, there is a WebView component in my fragment. I use it to load all webpages by the different urls, sort of stupid but efficient. As you know, we specify our webviewclient (like WvjbWebViewClient or jsBridge) to handle all requests. Now that we want to load third-platform websites with our WebView sometimes, however, we don't want to supply our business function for them, not for anything else, but for our safety of communication.
Our company domain is xxx.com. What I want to do is: when the webview loads those webpages on this domain, use our customer WebViewClient, otherwise use a simple WebViewClient(new a instance). How to resolve it? (Should we consider url redirection?)
WebViewClient allows you to upload any specified URL, selected in the WebView, in WebView itself, and do not run the browser. For this functionality meets shouldOverrideUrlLoading(WebView, String) method. If it returns true - we do not need to launch a third-party browser, and upload their own content here.
Here is an example where we choose if we can open the content in our app, or we need to open browser:
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith("xxx.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
This can help if think.

How do I open another app in my WebView app?

I have an Android app with that displays a mobile website (WebView), in the mobile website there are links redirecting to a PDF, Excel and video files.
When try to open it in my regular browser my phone asks to open it with another app or it start a download, so I can open it afterwards.
But in my WebView app it either doesn't work, no response or it displays a "Page unavailable" error.
Is it even possible?
To handle links in WebView, you can use the shouldOverrideUrlLoading method of WebViewClient class. Consider the following example;
WebView webView = (WebView) findViewById(R.id.infoView);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Assuming you are giving link to some PDF file.
if (url.contains(".pdf")) {
// Now do what you want to with the url here
}
return true;
}
}
This way, you can intercept any link tapped in WebView and then do whatever you want.

Android webview url redirect

I have a webview in my application which upon launch displays an html page. The page has a hyperlink which on click is supposed to display a video.
When i run the application and click on the video hyperlink link , nothing happens. But if i load the same page in android browser, then it launches a default video player and everything works fine.
I debugged it furthers by putting a log statement in shouldOverrideUrlLoading method and noticed that, when the hyperlink is clicked it gets redirected to another link and then to another link (final video streaming url).
My question is : why would the link work perfectly in default android browser and not through a webview.
Thanks
What is happening is when you click the hyperlink, that link probably has some popups inside of it. You need to define the onCreateWindow function in your webview's WebChromeClient. This handles how calls to open new windows or popups are handled.
public boolean onCreateWindow (WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
((WebView.WebViewTransport) resultMsg.obj).setWebView(myWebView);
resultMsg.sendToTarget();
return true;
}
After declaring your WebView you should set javascript enabled, then your WebView will work as a browser.
For example:
WebView mwebview = new WebView(this);
setContentView(mwebview);
mwebview.getSettings().setJavaScriptEnabled(true);
or
mwebview.getSettings().setPluginState(PluginState.ON); // this is for newer API's
Basically, do not expect your embedded WebView works the same as Android default Browser. The default Browser is built on the same WebView, but there are lots a customization. (Especially around the no-standard uri, HTML5 stuff)
I followed code from here: WebView and HTML5 <video>, and I put the video link to a video tag, and I got the Video playing in my own version of WebView. The behavior is a little different from the default Browser. Given more time, we could figure that out by looking at its code, but anyways ...

Android: Callbacks with WebView component?

I am doing authentication with a third-party site that's supposed to redirect back to my app with auth token (OAUTH).
I have the callback working properly if I open the 3rd party site in a separate browser process via
this.startActivity(new Intent(Intent.ACTION_VIEW, uri));
but, if I embed a WebView component in my layout, and open the url in that, the callback does not work. Webview says "You do not have permission to open myapp://callback?token=...." and quickly refreshes to "Web page not available...temporarily down...blah blah"
Any ideas?
You need to implement a WebViewClient to intercept the custom URI before it is loaded. The Hello, WebView tutorial shows a simple example. Where they have:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
You can change "view.loadUrl(url)" to check if the URL is your custom URL and handle it however you want.

Categories

Resources