【问题标题】:clear cookies in phonegap / disable phonegap from using cookies清除 phonegap 中的 cookie / 禁止 phonegap 使用 cookie
【发布时间】:2015-02-02 21:02:37
【问题描述】:

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

【问题讨论】:

    标签: javascript android cordova cookies


    【解决方案1】:

    在包“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");
                    }
                  );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2013-02-27
      相关资源
      最近更新 更多