How to show a Javascript permission popup in an Android WebView? - javascript

I am building an Android app with a WebView that shows a website. On 1 page on this website, it asks the user to allow camera permission (using getUserMedia() in Javascript). When I visit this page in a normal browser (pc or mobile), it correctly shows the popup where the user can allow or deny access. However, when visiting this page through the WebView in my app, this popup is not shown at all.
For the WebViewClient, I'm using this code:
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://example.com/");
WebSettings webSettings = webView.getSettings();
webSettings.setMediaPlaybackRequiresUserGesture(false);
webSettings.setJavaScriptEnabled (true);
I've also tried using the WebChromeClient, and the popup is shown there. However the problem with that is that it opens a whole new app (the Chrome browser) and I don't think there's a way to go back to my app programatically.
I have a few solutions in mind but I don't know which one works or is possible at all:
Only use WebViewClient and make it show JS popups. Is this possible?
Use WebChromeClient for the page that needs camera permission, and use WebViewClient for all other pages. Problem: How do I temporarily open the camera page and go back to the app when the user clicks allow/deny?
Any advice on this? Crazy workarounds are also very much welcome.

Related

Xamarin iOS HybridWebView app : use javascript function to auto click button after load Webview page

I am developing a Xamarin Android and iOS app, the app is using Webview to display the webpage content, I followed the article's tutorial:
Customizing a WebView
And there is a button in my Webview webpage, I want this app can automatically click the button when app finish load the Webview.
In Android project, I found how to do this , tested in Android simulator and it worked:
JavascriptWebViewClient.cs
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
view.EvaluateJavascript(_javascript, null);
view.LoadUrl("javascript:document.getElementById('btn_sign').click()");
}
but I don't know how to achieve this function in iOS project?
My iOS project has HybridWebViewRenderer.cs, I think maybe I need to do something similar,
but I can't solve this after did a lot of research.
Can anyone help me solve this?
Thanks!!!

Force to open my progressive web app in Chrome from WebView

I developed a Progressive Web App (JS+HTML+CSS), which asks you to "Add to Homescreen" and "Permission for Push Notification" when the user opens it. The problem is that when the URL is opened from a WebView, for example Facebook WebView, the service worker not works.
So I want a way to force the user to open the URL in Chrome mobile. I try using googlechrome://myurl.com or googlechromes://myurl.com but not works, and I also try to create a link like window.open('https://myurl.com, '_system') but not works.
Any ideas? Thanks!

how can I request a desktop site from inappbrowser cordova

I am requesting a website using cordova inappbrowser
http://bugdevstudios.com/pixi/
This website works correctly if we load it via desktop but not from mobile. Do you have any idea what might be the issue? You can view the live website as well. When I load it from mobile using "Request Desktop Website" in chrome or firefox (mobile) it works.
Inappbrowser doesn't support user-agent out of the box. Using a simple hack we may force inappbrowser to use cordova webview's user-agent-string.
Open your InAppBrowser.java and edit the run method.
WebSettings settings = inAppWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setBuiltInZoomControls(true);
settings.setPluginState(android.webkit.WebSettings.PluginState.ON);
settings.setUserAgentString(thatWebView.getSettings().getUserAgentString()); // magic line
Find more about "The Hack" here
Now to set cordova user agent string. we will use a plugin cordova-useragent
List of user agent string can be found here

WebView does not reload anymore on document.location change

I have a plain html / javascript App (no Phonegap) that i use with a native Android App and a WebView.
In the JavaScript part i have to reload the current page in some situations. With my old Phone and Android 4.3 i just used:
document.location = "index.html"
with my new Phone (Android 5) this seems not to work anymore. It stil works to change the page i.E. when i am currently viewing "books.html" i can use the above code to navigate to "index.html". But when i want to reload the same page i am currently viewing, nothing happens.
I debugged the WebView with the chrome debugger, no errors, no warnings, nothing. In chrome on my pc everything works fine.
Please Note:
I tried the solution mentioned by Rajesh. Unfortunately it didn't work out of the box.
I came across this issue:
https://bugs.chromium.org/p/chromium/issues/detail?id=327728
Its necessary to set a WebViewClient on the webView to make it work:
webView.setWebViewClient(new WebViewClient());
document.location
method will not reload page it essentially make entry to session history and make separate visit to page.
it may be possible that some browser implementation restrict adding side by side duplicate entry to session history, thus preventing reload of page.
best method to reload page is
document.location.reload()

WebView Not Loading a Specific Webpage

For a little context, I am currently working on an irc client based off of the open source kiwiirc web app. I'm building it to be cross platform so I have been doing some preliminary testing just to simply load the kiwiirc.com client in a web view.
Nothing fancy, just load the page up in a webview, and log in to an IRC server. On iOS using the standard UIKit frameworks, and for Desktop using Qt I have had great success.
On android not so much...
For some reason unbeknownst to me the webview on Android refuses to load the content for the address https://kiwiirc.com/client
It will load https://kiwiirc.com/ just fine, and in fact it will load any other website just fine as well. The only URL I have an issue with is the core one I need access to.
I've been trying to solve this problem for a few days now, and figured I'd come to the stack overflow community for some help since I am out of ideas.
The following is my code that I am currently using:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("https://kiwiirc.com/client");
}
}
Here are the following things I have tried in the order I have tried them, as well as their results, while I have been troubleshooting this issue:
Enabling Javascript - no affect on loading the webpage
Enabling DomStorage - no affect on loading the webpage
Embedding KiwiIRC in iFrame on another webpage - will load the webpage and kiwi embed PERFECTLY, but unfortunately I cannot fetch data from the iframe due to the "Same Origin" security policy.
EDIT
Relevant note: https://kiwiirc.com/client loads correctly via the android google chrome browser, but not in the webview of my app.
If anyone can offer me tips on troubleshooting this issue, provide me with a solution, or point out some obviously trivial thing I forgot to do, that would be amazing.
Thanks!

Categories