javascript - 在 WebView 中访问本地存储

标签 javascript android webview local-storage

首先,我看到这里还有其他可能相关的问题,但是提供的解决方案并没有解决我的问题。

为了尝试帮助解释问题,我将介绍我想要实现的目标;我们的网站会发送一封电子邮件,邀请用户注册我们的客户端应用程序。电子邮件中的链接会访问我们的网站,存储激活码,然后重定向到 Google Play 商店页面。用户下载应用程序并浏览介绍屏幕后,它会尝试通过加载 WebView 来查找激活代码。这就是问题所在,目前无法找到/访问本地存储来获取激活码。

这是我目前所拥有的:

应用商店重定向(电子邮件中的第一个链接)

    <html><head></head><body>
        <script type='text/javascript'>
            // The activation code goes in this url
            localStorage.setItem('url', '" + constructedUrl + "');
            setTimeout(function() { 
                window.location.href='BACK TO OUR SITE' }, 30000);
            setTimeout(function() { 
                window.location.href='GOOGLE PLAY STORE PAGE' }, 500);
        </script>
    </body></html>

AndroidManifest.xml 中的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

尝试访问激活码

cookieWebView.getSettings().setJavaScriptEnabled(true);
cookieWebView.getSettings().setAppCacheEnabled(false);
cookieWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
cookieWebView.getSettings().setDomStorageEnabled(true);
cookieWebView.setWebViewClient(new MyWebClient());
// Call the AppActivate.html file.
cookieWebView.loadUrl(ServerCommunications.getRedirectUrl());

cookieWebView.setWebChromeClient(new WebChromeClient() {
    public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
        Log.d("MyApplication", consoleMessage.message() + " -- From line "
                + consoleMessage.lineNumber() + " of "
                + consoleMessage.sourceId());
         return false;
    }
});

应用激活(从上面的WebView加载)

<head>
    <title></title>
</head>
<body>
    <script>
        var appUrl = localStorage.getItem('url');

        console.log("URL is ");
        console.log(appUrl);

        if (appUrl) {
            localStorage.removeItem('url');
            location.href = appUrl;
        } else {
            location.href = "OUR SCHEME://?token=0";
        }
    </script>
</body>
</html>

当应用程序尝试激活时,我看到的是以下两行记录:

09-18 09:06:30.151 4948-4948/our.namespace D/MyApplication: URL is  -- From line 10 of https://OURWEBSITE/AppActivate.html
09-18 09:06:30.152 4948-4948/our.namespace D/MyApplication: null -- From line 11 of https://OURWEBSITE/AppActivate.html

这表明该值未设置到本地存储中或者无法访问。我知道它已被存储,因为如果我从 Android 设备上的标准浏览器导航到上述 url,它就会为我提供我所期望的值。

我无法确定我是否缺少任何隐藏设置或者它是否不可能。我相信我已经按照可能相关问题的其他答案中的建议打开了与 WebView 的权限或设置相关的任何内容。如果有帮助的话,我正在 BlackView Android 6.0 手机上进行测试。

有什么明显的我遗漏的东西吗?

最佳答案

根据developer.android.com/reference/android/webkit/WebView.html :

For obvious security reasons, your application has its own cache, cookie store etc.—it does not share the Browser application's data. Cookies are managed on a separate thread, so operations like index building don't block the UI thread. Follow the instructions in CookieSyncManager if you want to use cookies in your application.

我建议的也许是使用深度链接。流程类似于:

  1. 像以前一样将激活码保存在本地存储中
  2. 当用户下载您的应用并完成入门流程时 包含一些打开网页的按钮(或自动执行) 系统浏览器中的页面。
  3. 现在您可以访问保存您的内容的本地存储空间 激活码。
  4. 在网站上提供一个包含激活码的深层链接按钮 在网址中
  5. 用户点击它并返回您的应用程序,您可以在其中抓取 Intent 中的激活码

如果您不想因离开应用而破坏用户体验,也许您应该查看推荐链接(搜索:Google Play 广告系列衡量)。从 Google Play 商店下载应用程序后,您可以在应用程序中访问推荐号码。在这种情况下,您可以忽略访问任何本地存储值并离开您的应用程序。

关于javascript - 在 WebView 中访问本地存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46280359/

相关文章:

javascript - 隐私 : Underscored prototype properties or encapsulated variables?

javascript - 对基于非 0 的数字范围取模

android - onKeyUp 不处理硬键盘的 Enter

Android Studio 未启动 : Fatal error initializing 'com. intellij.util.indexing.FileBasedIndex

android - Webview - 窗口已经聚焦,忽略了焦点增益

android - 将 URL 从 WebView 传递到 ShareActionProvider?

javascript - 在 JavaScript 中测试/检查字符串的最佳实践

javascript - 同步 AJAX 调用之前的代码在 Chrome 中卡住

android - GridView ,如 Windows 8 或 Google Play

javascript - Android 5.1 WebView javascript 问题