简体   繁体   中英

how to inject javascript localstorage.setItem to webview in android?

I have implemented webview in android and webview url also loaded succesddfully.

Now I need to inject javascript to webview. I need to set localstorage.setItem('key','value'). I found many suggestions but none of those are working.

here is code how i set webview and injecting javascript

webviewContactUs.getSettings().setJavaScriptEnabled(true);
    webviewContactUs.getSettings().setDomStorageEnabled(true);
    webviewContactUs.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webviewContactUs.getSettings().setLoadWithOverviewMode(true);
    webviewContactUs.getSettings().setSupportMultipleWindows(true);
    webviewContactUs.getSettings().setAllowFileAccess(true);
    webviewContactUs.getSettings().setAllowUniversalAccessFromFileURLs(true);
    webviewContactUs.addJavascriptInterface(javascriptInterfaceTest, "javascriptInterfaceTest");

    webviewContactUs.setWebViewClient(new MyWebViewClient());

    webviewContactUs.loadUrl(url);

    private class MyWebViewClient extends WebViewClient {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        callFunction1(view);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        callFunction1(view);

    }

}

private  void callFunction1(WebView view) {
    //webviewContactUs.addJavascriptInterface(javascriptInterfaceTest, "javascriptInterfaceTest");
    webviewContactUs.evaluateJavascript(javascriptInterfaceTest.getData(), new ValueCallback<String>() {
        @Override
        public void onReceiveValue(String value) {
            System.out.println("==== callFunction1 onReceiveValue 1 : " + value);
        }
    });
}

public class JavascriptInterfaceTest {
private Context mContext;


public JavascriptInterfaceTest(Context context) {
    mContext = context;
    System.out.println("===== JavascriptInterfaceTest");
}

@JavascriptInterface
public String getData() {
    String key = "\'key_abc\'";
    String val = "\'{\"token\":\"3b46a55ae773c48ad27e43ac7a649737e6ed70477f7562b102054253e5706318\"}\'";


    /*String key = "key_abc";
    String val = "{token:3b46a55ae773c48ad27e43ac7a649737e6ed70477f7562b102054253e5706318}";*/

    //String returnData = "window.sessionStorage.setItem(" + key + "," + val + ");";
    //String returnData = "window.localStorage.setItem(" + key + "," + val + ")";
    String returnData = "localStorage.setItem(" + key + "," + val + ")";
    //String returnData = "sessionStorage.setItem(" + key + "," + val + ");";

   
    System.out.println("==== returnData : " + returnData);
    return returnData;
 }


}

If this code will work then in url load with user data but some how this is not working because currently when url loaded opens login screen. So if any solution works then in webview, will not load log in screen.

One more thing is there is no any problem from web side because in IOS its working, here is ios implementation

在此处输入图像描述

As in image ios developer implemented. And in ios its working, same thing i want to be done in webview.

Any help would be appriciated. Thanks in advance.

 _webView.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                    _webView.evaluateJavascript("localStorage.setItem('testkey','testvalue');", null);
                } else {
                    _webView.loadUrl("javascript:localStorage.setItem('testkey','testvalue');");
                }
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM