Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After over a month of research I've concluded that setting a cookie in android versions greater than 2.3 is not possible in a webview where the file is on the localhost (read directly from the assets folder).</p> <p>I went through many alternatives to to using cookie storage, including using HTML's localstorage. The whole issue here, I am led to believe, is security. If I were to create a cookie or localstorage on the localhost, then other apps could access that storage, thus is a major security threat.</p> <p>My ultimate problem was trying to bridge the webview with two-way communication with java and also having a place to store data during the process. My solution was to take advantage of the <em>JavascriptInterface</em></p> <p>The idea is as follows:</p> <ul> <li>Parse data to function in javascript</li> <li>Javascript calls a special javascript function e.g. "setData(myDataName, myData)"</li> <li>Java has a function which listens to this, and will take arguments set by javscript, and can also return values to javascript.</li> <li>Once data is parsed to Java, java stores this in a series of files in assets.</li> <li>When javascript function "getData(myDataName)" is called, the data is returned by the same kind of method by Java.</li> </ul> <p>I found this very useful: <a href="http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface%28java.lang.Object,%20java.lang.String%29" rel="nofollow">Android <em>JavascriptInterface</em> Documentation</a>, <a href="http://www.jasonshah.com/handling-android-2-3-webviews-broken-addjavascriptinterface/" rel="nofollow">Handling JavascriptInterface in Android 2.3</a></p> <p>The only major 'hang-ups' come with a what I found was a <a href="http://code.google.com/p/android/issues/detail?id=12987" rel="nofollow">fairly serious bug</a> in Android 2.3 (fixed in 3.0) which effectively broke the JavascriptInterface. The second link above describes the best workaround I have found.</p> <p>Here is an example of how to get the two-way communication working (it's very messy, but it works):</p> <p>Custom webinterface class:</p> <pre><code>public class webViewInterface { //@JavascriptInterface //for SDK 12+ public String showToast(String myText) { Toast.makeText(context, myText, Toast.LENGTH_LONG).show(); return myText; } } </code></pre> <p>Main class: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);</p> <pre><code>setContentView(R.layout.activity_main); engine = (WebView) findViewById(R.id.web_engine); context = this; WebSettings webSettings = engine.getSettings(); webSettings.setJavaScriptEnabled(true); engine.setWebViewClient(new WebViewClient()); engine.loadUrl("file:///android_asset/" + page); boolean javascriptInterfaceBroken = false; try { if (Build.VERSION.RELEASE.startsWith("2.3")) { javascriptInterfaceBroken = true; } } catch (Exception e) { // Ignore, and assume user javascript interface is working correctly. } // Add javascript interface only if it's not broken // @TODO: write the workaround for &lt; 2.3 devices if (!javascriptInterfaceBroken) { engine.addJavascriptInterface(new webViewInterface(), "MainActivityInterface"); } //disables text selection engine.setOnLongClickListener(new View.OnLongClickListener() { public boolean onLongClick(View v) { return true; } }); } </code></pre> <p>HTML:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; &lt;input type="button" value="Say hello" onClick="showAndroidToast();" /&gt; &lt;script type="text/javascript"&gt; function showAndroidToast() { document.write(MainActivityInterface.showToast("eureka!")); } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload