├── README.md ├── package.json ├── plugin.xml ├── src ├── android │ └── CacheClear.java └── ios │ ├── CacheClear.h │ └── CacheClear.m └── www └── CacheClear.js /README.md: -------------------------------------------------------------------------------- 1 | Cache Clear 2 | ============= 3 | 4 | This is a WebView cache plugin for Cordova 6.1.1+ supporting Android (>=4.1) and iOS(>=6.0). 5 | It allows to clear the cordova webview cache. 6 | 7 | There is one method: 8 | 9 | * CacheClear(successCallback, errorCallback) 10 | 11 | Installation 12 | ====== 13 | You may use Cordova CLI as follows: 14 | 15 |
16 | cordova plugin add cordova-plugin-cache-clear
17 | 
18 | 19 | Usage 20 | ==== 21 | ```javascript 22 | document.addEventListener('deviceready', function() { 23 | var success = function(status) { 24 | alert('Message: ' + status); 25 | }; 26 | var error = function(status) { 27 | alert('Error: ' + status); 28 | }; 29 | window.CacheClear(success, error); 30 | }); 31 | ``` 32 | 33 | Android vs. iOS 34 | ====== 35 | 36 | On iOS, CacheClear deletes temporary files (images that have been downloaded by the app). 37 | On Android, CacheClear also deletes all local, persistent data (such as stored files and any data saved to localStorage). 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cordova-plugin-cache-clear", 3 | "version": "1.3.8", 4 | "author": { 5 | "name": "Anrip Wong", 6 | "email": "mail@anrip.com", 7 | "url": "http://www.anrip.com/" 8 | }, 9 | "homepage": "https://github.com/anrip/cordova-plugin-cache-clear", 10 | "description": "This is a WebView cache plugin for Cordova 6.1.1+ supporting Android (>=4.1) and iOS(>=6.0). It allows to clear the cordova webview cache.", 11 | "license": "Apache License Version 2.0", 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/anrip/cordova-plugin-cache-clear.git" 15 | }, 16 | "bugs": { 17 | "url": "https://github.com/anrip/cordova-plugin-cache-clear/issues" 18 | }, 19 | "keywords": [ 20 | "cordova", 21 | "webview", 22 | "cache", 23 | "clear", 24 | "ios", 25 | "android", 26 | "cordova-ios", 27 | "cordova-android" 28 | ], 29 | "cordova": { 30 | "id": "cordova-plugin-cache-clear", 31 | "platforms": [ 32 | "ios", 33 | "android" 34 | ] 35 | }, 36 | "engines": [ 37 | { 38 | "name": "cordova", 39 | "version": ">=6.1.1" 40 | } 41 | ] 42 | } 43 | -------------------------------------------------------------------------------- /plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Cordova Cache Clear 7 | cordova,webview,cache,clear,android,ios 8 | This is a WebView cache plugin for Cordova 6.1.1+ supporting Android (>=4.1) and iOS(>=6.0). It allows to clear the cordova webview cache. 9 | 10 | https://github.com/anrip/cordova-plugin-cache-clear.git 11 | https://github.com/anrip/cordova-plugin-cache-clear/issues 12 | Anrip Wong 13 | 14 | MIT 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /src/android/CacheClear.java: -------------------------------------------------------------------------------- 1 | package com.anrip.cordova; 2 | 3 | import org.json.JSONException; 4 | 5 | import org.apache.cordova.CallbackContext; 6 | import org.apache.cordova.CordovaArgs; 7 | import org.apache.cordova.CordovaPlugin; 8 | import org.apache.cordova.PluginResult; 9 | 10 | import android.annotation.TargetApi; 11 | import android.app.Activity; 12 | import android.util.Log; 13 | 14 | @TargetApi(19) 15 | public class CacheClear extends CordovaPlugin { 16 | 17 | private static final String LOG_TAG = "CacheClear"; 18 | private static final String MESSAGE_TASK = "Cordova Android CacheClear() called."; 19 | private static final String MESSAGE_ERROR = "Error while clearing webview cache."; 20 | 21 | @Override 22 | public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException { 23 | Log.v(LOG_TAG, MESSAGE_TASK); 24 | 25 | if (action.equals("task")) { 26 | task(callbackContext); 27 | return true; 28 | } 29 | 30 | return false; 31 | } 32 | 33 | public void task(CallbackContext callbackContext) { 34 | final CacheClear self = this; 35 | final CallbackContext callback = callbackContext; 36 | 37 | cordova.getActivity().runOnUiThread(new Runnable() { 38 | public void run() { 39 | try { 40 | // clear the cache 41 | self.webView.clearCache(true); 42 | // send success result to cordova 43 | PluginResult result = new PluginResult(PluginResult.Status.OK); 44 | result.setKeepCallback(false); 45 | callback.sendPluginResult(result); 46 | } catch (Exception e) { 47 | Log.e(LOG_TAG, MESSAGE_ERROR); 48 | // return error answer to cordova 49 | PluginResult result = new PluginResult(PluginResult.Status.ERROR, MESSAGE_ERROR); 50 | result.setKeepCallback(false); 51 | callback.sendPluginResult(result); 52 | } 53 | } 54 | }); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/ios/CacheClear.h: -------------------------------------------------------------------------------- 1 | #import 2 | 3 | #import 4 | 5 | #import "AppDelegate.h" 6 | 7 | @interface CacheClear : CDVPlugin { 8 | } 9 | 10 | - (void)task:(CDVInvokedUrlCommand *)command; 11 | 12 | // retain command for async repsonses 13 | @property(nonatomic, strong) CDVInvokedUrlCommand *command; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /src/ios/CacheClear.m: -------------------------------------------------------------------------------- 1 | #import "CacheClear.h" 2 | 3 | @implementation CacheClear 4 | 5 | @synthesize command; 6 | 7 | - (void)task:(CDVInvokedUrlCommand *)command 8 | { 9 | NSLog(@"Cordova iOS CacheClear() called."); 10 | 11 | self.command = command; 12 | 13 | [self.commandDelegate runInBackground:^{ 14 | [[NSURLCache sharedURLCache] removeAllCachedResponses]; 15 | [self success]; 16 | }]; 17 | } 18 | 19 | - (void)success 20 | { 21 | NSString *resultMsg = @"Cordova iOS webview cache cleared."; 22 | NSLog(@"%@", resultMsg); 23 | 24 | // create acordova result 25 | CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK 26 | messageAsString:[resultMsg stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 27 | 28 | // send cordova result 29 | [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; 30 | } 31 | 32 | - (void)error:(NSString *)message 33 | { 34 | NSString *resultMsg = [NSString stringWithFormat:@"Error while clearing webview cache (%@).", message]; 35 | NSLog(@"%@", resultMsg); 36 | 37 | // create cordova result 38 | CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR 39 | messageAsString:[resultMsg stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 40 | 41 | // send cordova result 42 | [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /www/CacheClear.js: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////// 2 | // CacheClear.js 3 | // Copyright (C) 2016 Anrip 4 | // 5 | ////////////////////////////////////////// 6 | 7 | var exec = require('cordova/exec'); 8 | 9 | var CacheClear = function (success, error) { 10 | exec(success, error, 'CacheClear', 'task', []); 11 | }; 12 | 13 | module.exports = CacheClear; 14 | --------------------------------------------------------------------------------