【问题标题】:Cookies are not being stored in Android WebviewCookie 未存储在 Android Webview 中
【发布时间】:2017-11-03 14:18:30
【问题描述】:

我有一个纯 HTML+CSS+JS 应用程序,我正在尝试使用 Webview 将其嵌入到 Android 应用程序中。该应用程序一切正常,只是没有存储 cookie。我已经尝试过这些 QA 中的建议,但它们似乎都不起作用:

Make Android WebView not store cookies or passwords

Cookie doesn't work properly in webview in android

Webview cannot accept cookies

Android WebView HTTP Cookies not working in API 21

这就是我在我的活动中启动 WebView 的方式:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    cookieManager.acceptCookie();
    cookieManager.setAcceptFileSchemeCookies(true);

    WebView webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);

    //webView.getSettings().setPluginState(PluginState.ON);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setAllowContentAccess(true);
    if (Build.VERSION.SDK_INT >= 16) {
        webView.getSettings().setAllowFileAccessFromFileURLs(true);
        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    }

    webView.loadUrl("file:///android_asset/index.html");

}

下面是 JavaScript createCookie() 方法,可以在普通浏览器中完美运行:

function createCookie(name, value, days) 
{
    value = value.replace(';', COOKIE_ENCODER);

    if (days>=0) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    //else var expires = "";


    document.cookie = name + "=" + value + expires; // + "; path=/";
}

我也尝试使用CookieSyncManager,尽管 Android Studio 建议它已贬值。调用 createCookie() 方法时,控制台不会显示任何错误,它只是不存储 cookie。

编辑

下面是readCookie() 函数的代码,用于在 JavaScript 中读取 cookie。我认为同样有可能存储 cookie,但浏览器在读取它时遇到问题:

function readCookie(name) 
{
    //name = name.replace(';',COOKIE_ENCODER);
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0)
        {
            s = c.substring(nameEQ.length, c.length);
            s = s.replace(COOKIE_ENCODER,';');
            return s;
        }
    }
    return null;
}

【问题讨论】:

    标签: javascript java android cookies webview


    【解决方案1】:

    您可以使用 window.localStorage 代替 cookie。 https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

    【讨论】:

    • 谢谢,格雷格。是的,这就是我最终在我的应用程序中所做的事情,但也必须有一种方法来保存 cookie,因为我们想用 Webview 模拟网络浏览器。
    • 您是否尝试过初始化 CookieManager onPageFinished,就像为已弃用的 CookieSyncManager 推荐的那样? stackoverflow.com/questions/8390156/…
    • 我做了,但 Android Studio 说 CookieSyncManager 已贬值。但无论如何我还是这样做了,即使这样也没有用(浏览器中仍然没有保存 cookie)。此外,我已经在我的设备和模拟器上测试了该应用程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多