javascript - 清除phonegap中的cookie/禁用phonegap使用cookie

标签 javascript android cordova cookies

我目前正在为我的移动应用程序制作一个登录系统。当我登录到我的服务器时,mu 服务器发回一个 JSESSIONID,我可以用它来验证自己的身份以进行进一步的调用。我在这里遇到的问题是,PhoneGap 似乎在没有我要求的情况下自动保存了这个 cookie。这样,我无法提供注销功能,因为 PhoneGap 正在保存 cookie 并始终在我进行的每次调用中将其发送回服务器......我正在使用 xmlhttp 请求。 我要么想要一种方法来删除这个 cookie,要么想要一种方法来禁止 PhoneGap 首先保存 cookie,然后自己处理 session cookie。 我一直在寻找这个 cookies ,但似乎找不到。 有人知道如何解决这个问题吗?

最佳答案

在包“org.apache.cordova.plugins”中创建类(CacheCleaner.java 文件)

package org.apache.cordova.plugins;

            import java.io.File;

            import org.apache.cordova.CallbackContext;
            import org.apache.cordova.CordovaPlugin;
            import org.apache.cordova.PluginResult;
            import org.json.JSONArray;

            import android.util.Log;
            import android.widget.Toast;

            public class CacheCleaner extends CordovaPlugin {

                public static final String LOG_PROV = "PhoneGapLog";
                public static final String LOG_NAME = "CacheCleaner Plugin";

                @Override
                public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext) {
                    if (action.equals("del")) {
                        cordova.getThreadPool().execute(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    final File dir = cordova.getActivity().getCacheDir();
                                    if (dir != null && dir.isDirectory()) {
                                        deleteDir(dir);
                                        showToast("Cache is deleted.","short");
                                    }
                                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    Log.e(LOG_PROV, LOG_NAME + ": Error: " + PluginResult.Status.ERROR);
                                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR));
                                }
                            }
                        });
                        return true;
                    } else {
                        Log.e(LOG_PROV, LOG_NAME + ": Error: " + PluginResult.Status.INVALID_ACTION);
                        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION));
                        return false;
                    }
                }

                public static boolean deleteDir(final File dir) {
                    if (dir != null && dir.isDirectory()) {
                        final String[] children = dir.list();
                        for (int i = 0; i < children.length; i++) {
                            final boolean success = deleteDir(new File(dir, children[i]));
                            if (!success) {
                                return false;
                            }
                        }
                    } else if (dir != null && dir.isFile()) {
                        dir.delete();
                    }
                    return true;
                }

                private void showToast(final String message, final String duration) {
                    cordova.getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(duration.equals("long")) {
                                Toast.makeText(cordova.getActivity(), message, Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(cordova.getActivity(), message, Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }

创建 JavaScript 文件“cachecleaner.js”

cordova.define("cordova/plugin/cachecleaner", function (require, exports, module) {
                var exec = require("cordova/exec");
              module.exports = {
                    del: function (win, fail) {
                    exec(win, fail, "CacheCleaner", "del", []);
                    }
                };
            });

将以下行添加到 Android 项目的 res 文件夹中的“config.xml”文件中。

<feature name="Share"><param name="android-package" value="org.apache.cordova.plugins.CacheCleaner" /></feature>

在您的 html 文件中使用此代码,当单击任何按钮时缓存都会被清除。

使用此功能:-

 $(document).on('click','.abs',function(){
             var cachecleaner = cordova.require("cordova/plugin/cachecleaner");
              cachecleaner.del(
                function () {
             console.log("PhoneGap Plugin: CacheCleaner: callback success"); 
              window.location.href = "index.html";             
                },
                function () {
                  console.log("PhoneGap Plugin: CacheCleaner: callback error");
                }
              );

关于javascript - 清除phonegap中的cookie/禁用phonegap使用cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28167342/

相关文章:

javascript - typescript:使用 typescript 和 webpack 2 解析 typeahead.js

java - 在 Android 中结合 Java 和 Kotlin

android - 在 ionic 中生成适用于 API 级别 19 的 Apk

javascript - 从 ng-option 获取值

javascript - ng-pattern 在 Angularjs 中验证个位数

javascript - 单元测试 UI 路由器嵌套状态

ios - 如何在phonegap/cordova插件中的webview后面插入 native View ?

android - 各个方向的应用程序

android - 获取纬度和经度的负值

iphone - XCode 4.2 + Iphone 3g 无法运行应用程序