1

I am connecting to url which loads only if token value is set properly to my webView browser.

This is a script where null value of userToken is found when I start my webView:

$.ajaxSetup({
    data: {
        token: localStorage.userToken
    }
});

sendToApp = function(_key, _val) {
    var iframe = document.createElement("IFRAME");
    iframe.setAttribute("src", _key + ":##sendToApp##" + _val);
    document.documentElement.appendChild(iframe);
    iframe.parentNode.removeChild(iframe);iframe = null;
}

IsMobile = true;

$.when(

    loadData.setup()

).then(function(data) {

    Setup = data;

    Struct = new App.Collection.Struct();
    View = new App.View.Phone();
    Zone = new App.Collection.Zone();

    Zone.setAll();
    Zone.findWhere({id:"0"}).set('path', 'Favourites')
    Struct.startLoop();
});

I receive error:

I/chromium: [INFO:CONSOLE(3)] "Uncaught TypeError: Cannot read property 'userToken' of null" , source: http://217.76.112.72:60180/js/mobile.js (3)

How can I set userToken to local storage of my android webView so this value won't be null anymore?

I read few threads about webView database or javascript parameters setting but I can't find answer to my question:

JS property setting:

Uncaught TypeError: Cannot set property 'value' of > null" in Android

Database topic:

Android webview & localStorage

Using local storage on Android webview

For example in those topics people are setting path to some kind of database which from what I understood is localStorage but if am I right how can I set value to it?

Edit: my webView setup:

public void setupWebView(String url, String token) {
    HashMap<String, String> map = new HashMap<>();
    map.put("userToken", token);

    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(url, map);
}
Community
  • 1
  • 1
F1sher
  • 7,140
  • 11
  • 48
  • 85
  • How is your WebView set up? This [answer might be appropriate](http://stackoverflow.com/a/5934650/503508). – Knossos Jan 18 '16 at 13:56
  • Hello, I have posted my webView setup. As you can see I am using .setDomStorageEnabled(true); but I don't know to which object/how should I insert my key/value so it can be read by script presented above. – F1sher Jan 18 '16 at 13:59

1 Answers1

0

Using the 2 parameter loadUrl won't help. You won't be able to access the headers.

What you CAN do, is you can call a function in your WebView.

webView.loadUrl("javascript:setUserToken(token)");

You can use this function to launch the rest of your code on the page too.

function setUserToken(userToken) {
    $.ajaxSetup({
        data: {
            token: userToken
        }
    });

    // ... Launch your ajax
}
Knossos
  • 15,802
  • 10
  • 54
  • 91
  • Perhaps it needs to be given a moment to load the WebView the first time. You could test it, delay the execution of the send loadUrl function by a few seconds. For example, by setting it on a button to press. – Knossos Jan 18 '16 at 14:43
  • sorry :D I've noticed that I need to add function to java with parameter but I don't have it – F1sher Jan 18 '16 at 14:49
  • 1
    Hello I've tried: webView.loadUrl("javascript:localStorage.setItem(\"userToken\",\"" + token + "\")"); But received: "Uncaught SecurityError: Failed to read the 'localStorage' property from 'Window': Access is denied for this document." – F1sher Jan 21 '16 at 10:28