简体   繁体   English

在WebView中访问本地存储

[英]Access local storage within WebView

First off I have seen there are other potentially related issues on here however the solutions offered have not solved my problem. 首先,我看到这里还有其他潜在的相关问题,但是提供的解决方案并未解决我的问题。

To try and help explain the problem I will cover what I am trying to achieve; 为了尝试帮助解释问题,我将介绍我要实现的目标; an email is sent from our website inviting a user to sign up with our client app. 我们的网站发送了一封电子邮件,邀请用户注册我们的客户端应用。 The link in the email hits our website, stores an activation code and then redirects to the Google Play Store page. 电子邮件中的链接会访问我们的网站,存储激活码,然后重定向到Google Play商店页面。 Once the user downloads the app and then works their way through the introduction screens it attempts to find the activation code by loading up a WebView. 一旦用户下载了该应用程序,然后在介绍屏幕上进行操作,它将尝试通过加载WebView来查找激活代码。 This is where things go wrong, currently it cannot find/access the local storage to get the activation code. 这是出问题的地方,当前它无法找到/访问本地存储以获取激活码。

Here is what I currently have in place: 这是我目前所拥有的:

App store redirection (the first link from the email) 应用商店重定向(电子邮件中的第一个链接)

    <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>

Permissions in the AndroidManifest.xml AndroidManifest.xml中的权限

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

Attempting to access activation code 尝试访问激活码

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;
    }
});

App activation (loaded from the above WebView) 应用激活(从上述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>

What I see when the app tries to activate is the following 2 lines logged: 当应用尝试激活时,我看到的是记录的以下2行:

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

Which shows that either the value wasn't set in to Local Storage or it can't be accessed. 这表明该值未设置为“本地存储”或无法访问。 I know that it has been stored because if I navigate to the above url from the standard browser on my Android device it gives me the value that I am expecting. 我知道它已存储,因为如果我从Android设备上的标准浏览器导航到上述url,它将为我提供期望的值。

I can't work out if I am missing any hidden setting or if it is just not possible. 如果我缺少任何隐藏设置,或者根本不可能,我将无法解决。 I believe I have turned on anything related to permissions or settings for the WebView as suggested in other answers to possibly related questions. 我相信我已经打开了与WebView权限或设置相关的所有内容,如对可能相关问题的其他答案所建议的。 If it helps I am testing on a BlackView Android 6.0 phone. 如果有帮助,我可以在BlackView Android 6.0手机上进行测试。

Is there anything obvious that I am missing? 有什么明显的我想念的东西吗?

According to developer.android.com/reference/android/webkit/WebView.html : 根据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. 出于明显的安全原因,您的应用程序具有自己的缓存,Cookie存储等-它不共享浏览器应用程序的数据。 Cookies are managed on a separate thread, so operations like index building don't block the UI thread. Cookies是在单独的线程上管理的,因此诸如索引构建之类的操作不会阻止UI线程。 Follow the instructions in CookieSyncManager if you want to use cookies in your application. 如果要在应用程序中使用cookie,请按照CookieSyncManager中的说明进行操作。

What I would suggest perhaps is to use deep linking. 我建议使用的是深层链接。 The flow would be something like: 流程将类似于:

  1. Save the activation code as before in the local storage 像以前一样将激活码保存在本地存储中
  2. When user downloads your app and goes thru the onboarding process include some button (or do it automatically) that opens up a web page in the system's browser. 当用户下载您的应用并进入启动过程时,请包括一些按钮(或自动执行),该按钮可在系统的浏览器中打开网页。
  3. Now you have access to the local storage where you saved your activation code. 现在,您可以访问保存激活代码的本地存储。
  4. On the website provide a deep linked button that has activation code in the url 在网站上,提供一个深层链接的按钮,该链接的网址中包含激活码
  5. User clicks it and goes back to your app where you can grab activation code from the Intent 用户单击它并返回到您的应用,您可以在其中从Intent中获取激活代码

If you don't want to break UX by leaving the app perhaps you should take a look at referral links (search for: Google Play Campaign Measurement ). 如果您不想离开应用程序来破坏用户体验,则应该查看引荐链接(搜索: Google Play Campaign Measurement )。 You can access referral number within your app upon app downloading from Google Play Store. 从Google Play商店下载应用后,您可以在应用中访问推荐编号。 In that case you can omit accessing any local storage values and leaving your app. 在这种情况下,您可以省略访问任何本地存储值并退出应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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