I have a pretty basic code for a webview app:
package com.budget.noname.budget;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String data = "<p id='v'></p><script>x=3; y=5; m=0; document.getElementById('v').innerHTML = m;</script>";
WebView simpleWebView=(WebView) findViewById(R.id.simpleWebView);
WebSettings webSettings = simpleWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowContentAccess(true);
webSettings.setAllowFileAccess(true);
webSettings.setDomStorageEnabled(true);
//simpleWebView.loadUrl("file:///android_asset/index.htm");
simpleWebView.loadDataWithBaseURL(null, data, "text/html", "UTF-8", "");
}
}
If I put my webapp (index.htm) in the assets folder and load it, it works perfectly, but my code is available for anyone who cares to extract the .apk.
I was trying to paste the code on a String and the load it with loadDataWithBaseURL. It worked almost as well. The thing is: if I try to access the localStorage, the code breaks. Why is that?
Example:
String data = "<script>x=localStorage.getItem('name');</script>";
Doesn't work!!! Although, as I said, if I load the same code from the assets folder, it works.