diff --git a/.gitignore b/.gitignore index 648a292..4fb7525 100644 --- a/.gitignore +++ b/.gitignore @@ -1,26 +1,4 @@ -# built application files -*.apk -*.ap_ - -# files for the dex VM -*.dex - -# Java class files -*.class - -# generated files -bin/ -gen/ - -# Local configuration file (sdk path, etc) -local.properties - -# Eclipse project files -.classpath -.project - -# Proguard folder generated by Eclipse -proguard/ +.DS_Store # Intellij project files *.iml diff --git a/README.md b/README.md index 58bdc9e..2ef34aa 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,27 @@ -Cookies +Phonegap Cookies Plugin ======= -Phonegap Cookies plugin +Phonegap/Cordova plugin that allows you to clear cookies of the webview. Use it for logging out the user, restart analytics session etc. + +## Why a plugin? + +On Phonegap `document.cookie` is empty, index.html and all other files are loaded with `file://` protocol. +Phonegap manages cookies internally, but doesn't expose any function for clearing them. + +## Installation: + +### Automatically (CLI / Plugman) + +Cookies is compatible with [Cordova Plugman](https://github.com/apache/cordova-plugman) and ready for the [PhoneGap 3.0 CLI](http://docs.phonegap.com/en/3.0.0/guide_cli_index.md.html#The%20Command-line%20Interface_add_features), here's how it works with the CLI: + +``` +$ phonegap local plugin add https://github.com/bez4pieci/Cookies.git +``` + +## Usage + +```javascript +window.cookies.clear(function() { + console.log('Cookies cleared!'); +}); +``` diff --git a/package.json b/package.json new file mode 100644 index 0000000..f3f10e2 --- /dev/null +++ b/package.json @@ -0,0 +1,13 @@ +{ + "version": "0.0.1", + "name": "com.bez4pieci.cookies", + "cordova_name": "Cookies", + "description": "Cordova Cookies Plugin", + "license": "Apache 2.0", + "keywords": [ + "cordova", + "phonegap", + "cookies" + ], + "engines": [] +} \ No newline at end of file diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..4ce1b15 --- /dev/null +++ b/plugin.xml @@ -0,0 +1,38 @@ + + + Cookies + Cordova Device Plugin + MIT + cordova,phonegap,cookies + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/android/Cookies.java b/src/android/Cookies.java new file mode 100644 index 0000000..3030658 --- /dev/null +++ b/src/android/Cookies.java @@ -0,0 +1,57 @@ +/* + * Copyright 2013 Ernests Karlsons + * https://github.com/bez4pieci + * http://www.karlsons.net + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + + +package com.bez4pieci.cookies; + +import org.apache.cordova.CallbackContext; +import org.apache.cordova.CordovaPlugin; +import org.json.JSONArray; +import org.json.JSONException; + +import android.util.Log; +import android.webkit.CookieManager; + +public class Cookies extends CordovaPlugin { + + private final String TAG = "CookiesPlugin"; + + @Override + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + if ("clear".equals(action)) { + this.clear(); + callbackContext.success(); + return true; + } + return false; // Returning false results in a "MethodNotFound" error. + } + + public void clear() { + Log.v(TAG, "Clearing cookies..."); + CookieManager.getInstance().removeAllCookie(); + } + + +} diff --git a/src/ios/CDVCookies.h b/src/ios/CDVCookies.h new file mode 100644 index 0000000..7597f97 --- /dev/null +++ b/src/ios/CDVCookies.h @@ -0,0 +1,15 @@ +// +// CDVCookies.h +// Dreamflat +// +// Created by Dr. E on 25/11/13. +// +// + +#import + +@interface CDVCookies : CDVPlugin + +- (void)clear:(CDVInvokedUrlCommand*)command; + +@end diff --git a/src/ios/CDVCookies.m b/src/ios/CDVCookies.m new file mode 100644 index 0000000..8f1593a --- /dev/null +++ b/src/ios/CDVCookies.m @@ -0,0 +1,43 @@ +/* + * Copyright 2013 Ernests Karlsons + * https://github.com/bez4pieci + * http://www.karlsons.net + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#import "CDVCookies.h" + +@implementation CDVCookies + +- (void)clear:(CDVInvokedUrlCommand*)command +{ + NSHTTPCookie *cookie; + NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; + for (cookie in [storage cookies]) { + [storage deleteCookie:cookie]; + } + [[NSUserDefaults standardUserDefaults] synchronize]; + + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + +@end diff --git a/www/cookies.js b/www/cookies.js new file mode 100644 index 0000000..116c99b --- /dev/null +++ b/www/cookies.js @@ -0,0 +1,46 @@ + +/* + * Copyright 2013 Ernests Karlsons + * https://github.com/bez4pieci + * http://www.karlsons.net + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +var exec = require('cordova/exec'); + +/** + * @constructor + */ +function Cookies() { + +} + +/** + * Get device info + * + * @param {Function} successCallback The function to call when cookies cleared successfully + * @param {Function} errorCallback The function to call when there was an error (OPTIONAL) + */ +Cookies.prototype.clear = function(successCallback, errorCallback) { + exec(successCallback, errorCallback, "Cookies", "clear", []); +}; + +module.exports = new Cookies();