├── .clang-format ├── .gitignore ├── LICENSE ├── README.md ├── package.json ├── plugin.xml ├── src ├── android │ └── Cache.java └── ios │ ├── Cache.h │ └── Cache.m └── www └── Cache.js /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: LLVM 2 | IndentWidth: 8 3 | UseTab: Always 4 | BreakBeforeBraces: Linux 5 | AllowShortIfStatementsOnASingleLine: false 6 | IndentCaseLabels: false 7 | ColumnLimit: 120 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright © 2014-2015 Andrew Stevens , 3 | Modern Alchemits 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the “Software”), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cache Clear 2 | 3 | This is a WebView cache plugin for Cordova 5.4.0 supporting Android (>=2.3.3) and iOS(>=6.0) 4 | It allows the app to use javascript to initiate a cordova webview cache clear 5 | 6 | There are two methods: 7 | 8 | `clear(successCallback, errorCallback)` 9 | `cleartemp()` 10 | 11 | #### Manual Installation 12 | 13 | You may use `cordova-cli` as follows: 14 | 15 | ```shell 16 | cordova plugin add https://github.com/andxyz/cordova-plugin-cache.git 17 | ``` 18 | 19 | #### Usage 20 | 21 | ```javascript 22 | document.addEventListener('deviceready', onDeviceReady); 23 | 24 | function onDeviceReady() { 25 | var success = function(status) { 26 | alert('Message: ' + status); 27 | } 28 | 29 | var error = function(status) { 30 | alert('Error: ' + status); 31 | } 32 | 33 | window.cache.clear(success, error); 34 | window.cache.cleartemp(); 35 | } 36 | 37 | ``` 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cordova-plugin-cache", 3 | "version": "1.0.5", 4 | "description": "Cordova Clear Cache Plugin", 5 | "cordova": { 6 | "id": "cordova-plugin-cache", 7 | "platforms": [ 8 | "android", 9 | "ios" 10 | ] 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/andxyz/cordova-plugin-cache" 15 | }, 16 | "keywords": [ 17 | "cordova", 18 | "cache", 19 | "ecosystem:cordova", 20 | "cordova-android", 21 | "cordova-ios" 22 | ] 23 | } 24 | -------------------------------------------------------------------------------- /plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | Clear Cache 8 | https://github.com/andxyz/cordova-plugin-cache.git 9 | webview,cache,clear,android,ios 10 | MIT 11 | andxyz 12 | 13 | <p>This is a WebView cache plugin for Cordova 5.4.0 supporting Android (>=2.3.3) and iOS(>=6.0). It allows to clear the cordova webview cache.</p> 14 | 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 | -------------------------------------------------------------------------------- /src/android/Cache.java: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | Copyright © 2014-2015 Andrew Stevens , Modern Alchemits 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 6 | documentation files (the "Software"), to deal in the Software without restriction, including without limitation 7 | the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 8 | to permit persons to whom the Software is furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of 11 | the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 14 | THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 15 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 16 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 17 | SOFTWARE. 18 | */ 19 | 20 | package org.apache.cordova.plugin.cache; 21 | 22 | import org.apache.cordova.CallbackContext; 23 | import org.apache.cordova.CordovaPlugin; 24 | import org.apache.cordova.PluginResult; 25 | 26 | import org.json.JSONArray; 27 | import org.json.JSONException; 28 | 29 | import java.io.File; 30 | 31 | import android.annotation.TargetApi; 32 | import android.app.Activity; 33 | import android.util.Log; 34 | 35 | @TargetApi(19) 36 | public class Cache extends CordovaPlugin { 37 | private static final String LOG_TAG = "Cache"; 38 | private CallbackContext callbackContext; 39 | 40 | /** 41 | * Constructor. 42 | */ 43 | public Cache() { 44 | 45 | } 46 | 47 | @Override 48 | public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 49 | /*try 50 | { 51 | */ 52 | if ( action.equals("clear") ) { 53 | Log.v(LOG_TAG, "Cordova Android Cache.clear() called."); 54 | this.callbackContext = callbackContext; 55 | 56 | final Cache self = this; 57 | cordova.getActivity().runOnUiThread( new Runnable() { 58 | public void run() { 59 | try { 60 | // clear the cache 61 | self.webView.clearCache(true); 62 | 63 | // clear the data 64 | self.clearApplicationData(); 65 | 66 | // send success result to cordova 67 | PluginResult result = new PluginResult(PluginResult.Status.OK); 68 | result.setKeepCallback(false); 69 | self.callbackContext.sendPluginResult(result); 70 | } catch ( Exception e ) { 71 | String msg = "Error while clearing webview cache."; 72 | Log.e(LOG_TAG, msg ); 73 | 74 | // return error answer to cordova 75 | PluginResult result = new PluginResult(PluginResult.Status.ERROR, msg); 76 | result.setKeepCallback(false); 77 | self.callbackContext.sendPluginResult(result); 78 | } 79 | } 80 | }); 81 | return true; 82 | } 83 | return false; 84 | /* 85 | } 86 | catch (JSONException e) 87 | { 88 | // TODO: signal JSON problem to JS 89 | //callbackContext.error("Problem with JSON"); 90 | return false; 91 | } 92 | */ 93 | } 94 | 95 | // http://www.hrupin.com/2011/11/how-to-clear-user-data-in-your-android-application-programmatically 96 | private void clearApplicationData() { 97 | File cache = this.cordova.getActivity().getCacheDir(); 98 | File appDir = new File(cache.getParent()); 99 | Log.i(LOG_TAG, "Absolute path: " + appDir.getAbsolutePath()); 100 | if (appDir.exists()) { 101 | String[] children = appDir.list(); 102 | for (String s : children) { 103 | if (!s.equals("lib")) { 104 | deleteDir(new File(appDir, s)); 105 | Log.i(LOG_TAG, "File /data/data/APP_PACKAGE/" + s + " DELETED"); 106 | } 107 | } 108 | } 109 | } 110 | 111 | private static boolean deleteDir(File dir) { 112 | Log.i(LOG_TAG, "Deleting: " + dir.getAbsolutePath()); 113 | if (dir != null && dir.isDirectory()) { 114 | String[] children = dir.list(); 115 | for (int i = 0; i < children.length; i++) { 116 | boolean success = deleteDir(new File(dir, children[i])); 117 | if (!success) { 118 | return false; 119 | } 120 | } 121 | } 122 | 123 | return dir.delete(); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/ios/Cache.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2014-2015 Andrew Stevens , 3 | Modern Alchemits 4 | 5 | Licensed under MIT. 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 8 | documentation files (the "Software"), to deal in the Software without restriction, including without limitation the 9 | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit 10 | persons to whom the Software is furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the 13 | Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 16 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 18 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | */ 20 | 21 | #import "AppDelegate.h" 22 | #import 23 | #import 24 | 25 | @interface Cache : CDVPlugin { 26 | NSString* _callbackId; 27 | } 28 | 29 | - (void)clear:(CDVInvokedUrlCommand *)command; 30 | - (void)cleartemp:(CDVInvokedUrlCommand *)command; 31 | 32 | // retain command for async repsonses 33 | @property(nonatomic, strong) CDVInvokedUrlCommand *command; 34 | 35 | @end 36 | -------------------------------------------------------------------------------- /src/ios/Cache.m: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2014-2015 Andrew Stevens , 3 | Modern Alchemits 4 | 5 | Licensed under MIT. 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 8 | documentation files (the "Software"), to deal in the Software without restriction, including without limitation the 9 | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit 10 | persons to whom the Software is furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the 13 | Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 16 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 18 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | */ 20 | 21 | #import "Cache.h" 22 | 23 | @implementation Cache 24 | 25 | @synthesize command; 26 | 27 | - (void)clear:(CDVInvokedUrlCommand *)command 28 | { 29 | NSLog(@"Cordova Cache Plugin method clear() called."); 30 | 31 | _callbackId = command.callbackId; 32 | 33 | // Plugin arguments are not used at the moment. 34 | // NSArray* arguments = command.arguments; 35 | 36 | [self.commandDelegate runInBackground:^{ 37 | // clear cache 38 | [[NSURLCache sharedURLCache] removeAllCachedResponses]; 39 | }]; 40 | 41 | [self success:@"NSURLCache"]; 42 | } 43 | 44 | - (void)cleartemp:(CDVInvokedUrlCommand *)command 45 | { 46 | NSLog(@"Cordova Cache Plugin method cleartemp() called."); 47 | 48 | _callbackId = command.callbackId; 49 | 50 | // empty the tmp directory 51 | NSFileManager *fileMgr = [[NSFileManager alloc] init]; 52 | // setup loop vars 53 | NSError *err = nil; 54 | BOOL hasErrors = NO; 55 | NSString *tempDirectoryPath = NSTemporaryDirectory(); 56 | NSDirectoryEnumerator *directoryEnumerator = [fileMgr enumeratorAtPath:tempDirectoryPath]; 57 | NSString *fileName = nil; 58 | BOOL result; 59 | // clear contents of NSTemporaryDirectory 60 | while ((fileName = [directoryEnumerator nextObject])) { 61 | NSString *filePath = [tempDirectoryPath stringByAppendingPathComponent:fileName]; 62 | result = [fileMgr removeItemAtPath:filePath error:&err]; 63 | if (!result && err) { 64 | NSLog(@"Failed to delete: %@ (error: %@)", filePath, err); 65 | hasErrors = YES; 66 | } 67 | } 68 | 69 | // send result 70 | if (hasErrors) { 71 | [self error:@"NSTemporaryDirectory"]; 72 | } 73 | else { 74 | [self success:@"NSTemporaryDirectory"]; 75 | } 76 | } 77 | 78 | - (void)success:(NSString *)message 79 | { 80 | NSString *resultMsg = [NSString stringWithFormat:@"Cordova Cache Plugin cleared the cache: (%@).", message]; 81 | NSLog(@"%@", resultMsg); 82 | 83 | // create cordova result 84 | CDVPluginResult *pluginResult = [CDVPluginResult 85 | resultWithStatus:CDVCommandStatus_OK 86 | messageAsString:[resultMsg stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 87 | 88 | // send cordova result 89 | [self.commandDelegate sendPluginResult:pluginResult callbackId:_callbackId]; 90 | } 91 | 92 | - (void)error:(NSString *)message 93 | { 94 | NSString *resultMsg = [NSString stringWithFormat:@"Cordova Cache Plugin error clearing: (%@).", message]; 95 | NSLog(@"%@", resultMsg); 96 | 97 | // create cordova result 98 | CDVPluginResult *pluginResult = [CDVPluginResult 99 | resultWithStatus:CDVCommandStatus_ERROR 100 | messageAsString:[resultMsg stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 101 | 102 | // send cordova result 103 | [self.commandDelegate sendPluginResult:pluginResult callbackId:_callbackId]; 104 | } 105 | 106 | - (void)onPause 107 | { 108 | NSLog(@"Cordova Cache Plugin onPause() fired"); 109 | // [self clear]; 110 | // [self cleartemp]; 111 | } 112 | 113 | - (void)onResume 114 | { 115 | NSLog(@"Cordova Cache Plugin onResume() fired"); 116 | // [self clear]; 117 | // [self cleartemp]; 118 | } 119 | 120 | - (void)pluginInitialize 121 | { 122 | if (UIApplicationDidEnterBackgroundNotification && UIApplicationWillEnterForegroundNotification) { 123 | [[NSNotificationCenter defaultCenter] addObserver:self 124 | selector:@selector(onPause) 125 | name:UIApplicationDidEnterBackgroundNotification 126 | object:nil]; 127 | [[NSNotificationCenter defaultCenter] addObserver:self 128 | selector:@selector(onResume) 129 | name:UIApplicationWillEnterForegroundNotification 130 | object:nil]; 131 | } 132 | } 133 | 134 | @end 135 | -------------------------------------------------------------------------------- /www/Cache.js: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////// 2 | // Cache.js 3 | // 4 | // The MIT License (MIT) 5 | // Copyright © 2014-2015 Andrew Stevens , 6 | // Modern Alchemits 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the “Software”), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | 18 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | ////////////////////////////////////////// 27 | 28 | var exec = require('cordova/exec'); 29 | 30 | var Cache = { 31 | clear: function(success, error) { 32 | exec(success, error, "Cache", "clear", []) 33 | }, 34 | cleartemp: function(success, error) { 35 | exec(success, error, "Cache", "cleartemp", []) 36 | } 37 | } 38 | 39 | module.exports = Cache; 40 | --------------------------------------------------------------------------------