I am trying to access this url in an Android WebView: https://www.tokopedia.com/
I am not able to, I just get the message: Blocked WebView
Is there any way around this?
I have set
webView.settings.domStorageEnabled = true
webView.settings.javaScriptEnabled = true
webView.settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
Related
I am having a pdf url from amazon s3 bucket and I am trying to open that with my webview but it doesn't works and it shows only blank screen.
Eg Url - https://XYZwest-1.amazonaws.com/XYZ.pdf?X-Amz-Expires=86400&x-amz-security-token={TOKEN HERE}&X-Amz-Algorithm={Algorithn}Amz-Credential={AWS CREDS}&X-Amz-Date=20220815T154223Z&X-Amz-SignedHeaders=host;x-amz-security-token&X-Amz-Signature={Signature}
This is how I have my webview setup -
val settings = binding.webView.settings
// Enable java script in web view
settings.javaScriptEnabled = true
binding.webView.setLayerType(View.LAYER_TYPE_HARDWARE, null)
binding.webView.settings.domStorageEnabled = true
binding.webView.loadUrl(webUrl)
```.
I have created a web app in Android studio using WebView. I have loaded a WordPress site in android WebView. The WordPress site exports a pdf report on a button click When i load it on chrome browser.
The website loaded in chrome
When i click on PDF button shows the window
and
and finally it starts downloading the pdf file
and the generated pdf report looks like:
But the problem occurs when i use the Android App created in WebView. When i click on PDF button it does nothing and i get a Debug log
D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: true
[InputMethodManagerWrapper.java:68] hideSoftInputFromWindow
I have manifest permission set to
uses-permission android:name="android.permission.INTERNET"
Code here
private static final String WEB_URL = "http://workmanager.midzonetechnologies.com/";
webView = findViewById(R.id.webView);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
webView.getSettings().setBlockNetworkLoads(false);
webView.getSettings().setAppCacheMaxSize( 10 * 1024 * 1024 ); // 10MB
webView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath() );
webView.getSettings().setAllowFileAccess( true );
webView.getSettings().setAppCacheEnabled( true );
webView.getSettings().setJavaScriptEnabled( true );
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(WEB_URL);
Please tell is the problem related to manifest file permission or run time permission, Please give a solution.
The Android app that i created in WebView.
What you are seeing happening in Chrome is:
noticing the downloadable file based on path: mywebsite.com/randomPath/randomFile**.pdf**
request download permissions
download the file to it's local storage
open the PDF file using Google Drive
If you want that behaviour you have to code all those steps in your app.
A fast solution to this is to intercept the url, check if it is a pdf file and then use a browser to open it.
webView.webviewClient = object : WebViewClient() {
#TargetApi(Build.VERSION_CODES.N)
fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest): Boolean {
val url = request.url.toString()
if (url.takeLast(4) == ".pdf") {
startActivity(
Intent(
Intent.ACTION_VIEW,
"https://docs.google.com/gview?embedded=true&url=${request.url}"
)
)
return true
}
return false
}
}
You would probably have to have different steps in here. As #Florin T. stated, you would first have to get the Permission required for your App:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
In the next step you would be detect wether the system can be downloading anything.
This coud be done like so:
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}});
Source here
If you then click on the PDF-Button in your WebView, the system should be handling the download on your phone. This means that you can then be opening your download as if it would have been downloaded via chrome.
In the last step, you would have to get the saved loaction from the download and start an Intent to open it with a local PDF-App. This could be done via android intents.
In my app I want the user to read the pdf files inside my app without any option to download them. How can I do this? I am using the following code but not working-
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.loadUrl("https://docs.google.com/viewer?"+pdf_url);
Is there any other way to do this? If you can do it?
You can open pdf from URL online in Android without a download option.
Kotlin
val path="https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf"
activityWebViewBinding.webview.settings.loadWithOverviewMode = true
activityWebViewBinding.webview.settings.javaScriptEnabled = true
val url = "https://docs.google.com/gview?embedded=true&url=$path"
activityWebViewBinding.webview.loadUrl(url)
Java
String path="https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf";
webview.settings.setLoadWithOverviewMode(true);
webview.settings.setJavaScriptEnabled(true);
String url = "https://docs.google.com/gview?embedded=true&url="+path;
webview.loadUrl(url);
You can simply launch intent as below
val pdfUrl = "ENTER_PDF_URL_HERE"
val intent: Intent = Intent(Intent.ACTION_VIEW, Uri.parse(pdfUrl))
.addCategory(Intent.CATEGORY_BROWSABLE)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
This would simply open it up device's pdf viewer app which comes in almost all android devices com.google.android.apps.docs
You can also safeguard this code by querying packages beforehand and adding fallbacks
Let's say, this is the link I'm using to show in android webview using KOTLIN. But, Only a blank page is coming.
https://vimeo.com/user92933894/review/307846259/0b380ce589
Now, here is my code:
var url = intent.getStringExtra("link")
println(url)
val videoPreferences = VideoPreferences(this)
val pointPreferences = PointPrefernce(this)
var videoIds = videoPreferences.getVideoId()
paidvideo.settings.javaScriptEnabled=true
paidvideo.settings.setSupportZoom(true)
paidvideo.webViewClient = object : WebViewClient(){
}
paidvideo.loadUrl(url)
Here the url is getting the link as extras of an intent. And the link is coming perfectly. And youtube link is working perfectly, but not the vimeo's one. BTW, I don't need autoplay option.
At last I found the answer. Actually, here "Uncaught TypeError: Cannot call method 'getItem' of null" error happened. and to solve this error, I needed to add this following line.
paidvideo.settings.domStorageEnabled = true
making domStorageEnabled true solves the problem.
I've implemented a HybridWebView as described here: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/hybridwebview/
For now, I have the url hardcoded in the renderer, like this:
Control.LoadUrl("http://www.google.com");
The result is the url opens, but always in a new browser, not embedded in my ContentPage as I would expect.
Is there a Settings property on the Android.Webkit.WebView I can use to determine how it displays?
The result is the url opens, but always in a new browser, not embedded in my ContentPage as I would expect.
For android platform, it uses Android.Webkit.WebView as native control as you can see it from code:
if (Control == null) {
var webView = new Android.Webkit.WebView (Forms.Context);
webView.Settings.JavaScriptEnabled = true;
SetNativeControl (webView);
}
Then for Android.Webkit.WebView, if you don't enable the activity to handle the intent to view a web page, it is a simple web page viewer, not quite a browser yet because as soon as you click a link, the default Android Browser handles it. To enable it, this code can go anywhere following the initialization of WebView: webView.SetWebViewClient(new WebViewClient()). So you may add this code for example:
if (Control == null) {
var webView = new Android.Webkit.WebView (Forms.Context);
webView.SetWebViewClient(new WebViewClient());
webView.Settings.JavaScriptEnabled = true;
SetNativeControl (webView);
}
Usually we can also subclass WebViewClient and override ShouldOverrideUrlLoading, if it returns true, means that method has handled the URL and the event should not propagate.