I have a simple HTML file:
<html>
<head>
<script async src="https://URL"></script>
</head>
<body>
<div class="class-name"></div>
</body>
</html>
When I access it with a browser using Windows: C:\foo.html?q=query the file loads correctly.
When I access the same file, that I have added it to my Android project in the Assets directory, with a Custom WebView and a Custom Renderer in my Xamarin.Forms project like this: myCustomWebview.Uri = "foo.html" + "?q=" + queryString; the Javascript fails to populate the div in the body correctly. The same problem exists if I load the file without a query string: myCustomWebview.Uri = "foo.html";
What's the deal? Do I need to set any special Permissions in the AndroidManifest.xml for the Javascript to work properly? Do I need to set a specific BuildAction in the file properties of the HTML file in my Android project? Do I need to do something else?
Place html file into Assets directory with build action AndroidAsset .
Use DependencyService to set the exact path .
[assembly: Dependency (typeof(BaseUrl_Android))]
namespace WorkingWithWebview.Android
{
public class BaseUrl_Android : IBaseUrl
{
public string Get()
{
return "file:///android_asset/";
}
}
}
Set BaseUrl to HtmlWebViewSource and pass it to WebView.Source .
var source = new HtmlWebViewSource();
source.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
webView.Source = source .
Refer to