Created
May 4, 2016 15:18
-
-
Save nothingatalldotnet/5de4b02ae3679b44a85eb79084d8152c to your computer and use it in GitHub Desktop.
Cordova Android SharedPreferences / iOS KeyChain basic bits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = { | |
settings : { | |
device_platform : '', | |
storage_name : 'some-prefs' | |
}, | |
initialize: function() { | |
this.bindEvents(); | |
}, | |
bindEvents: function() { | |
document.addEventListener('deviceready', this.onDeviceReady, false); | |
document.getElementById("secure-store").addEventListener("click", function() { | |
app.storageAddPassword("string","test-password","SOMEPASSWORD"); | |
}, false); | |
document.getElementById("secure-retrieve").addEventListener("click", function() { | |
app.storageRetrievePassword("string","test-password"); | |
}, false); | |
document.getElementById("secure-remove").addEventListener("click", function() { | |
app.storageRemovePassword("test-password"); | |
}, false); | |
}, | |
onDeviceReady: function() { | |
app.receivedEvent('deviceready'); | |
app.settings.device_platform = device.platform; | |
app.initSecureStorage(); | |
}, | |
/** | |
* iOS SECURE STORAGE | |
* http://plugins.telerik.com/cordova/plugin/keychain | |
* cordova plugin add https://github.com/Telerik-Verified-Plugins/Keychain | |
* | |
* ANDROID SECURE STORAGE | |
* https://github.com/edelworksgithub/SharedPreferences | |
* cordova plugin add https://github.com/edelworksgithub/SharedPreferences.git | |
*/ | |
initSecureStorage: function() { | |
if (app.settings.device_platform === "iOS") { | |
console.log("secure storage using apple keychain"); | |
} else if (app.settings.device_platform === "Android"){ | |
sharedpreferences.getSharedPreferences(app.settings.storage_name, "MODE_PRIVATE", app.storageSuccessHandler, app.storageErrorHandler); | |
console.log("secure storage using android sharedpreferences"); | |
} else { | |
console.log("unsupported device"); | |
} | |
}, | |
storageAddPassword: function(type,key,value) { | |
if (app.settings.device_platform === "iOS") { | |
new Keychain().setForKey(app.storageSuccessHandler, app.storageErrorHandler, key, app.settings.storage_name, value); | |
} else if (app.settings.device_platform === "Android"){ | |
switch(type){ | |
case "string": | |
sharedpreferences.putString(key, value, app.storageSuccessHandler, app.storageErrorHandler); | |
break; | |
case "int": | |
sharedpreferences.putInt(key, value, app.storageSuccessHandler, app.storageErrorHandler); | |
break; | |
case "long": | |
sharedpreferences.putLong(key, value, app.storageSuccessHandler, app.storageErrorHandler); | |
break; | |
case "boolean": | |
sharedpreferences.putBoolean(key, value, app.storageSuccessHandler, app.storageErrorHandler); | |
break; | |
} | |
} else { | |
console.log("unsupported device"); | |
} | |
}, | |
storageRetrievePassword: function(type,key) { | |
if (app.settings.device_platform === "iOS") { | |
new Keychain().getForKey(app.storageSuccessHandler, app.storageErrorHandler, key, app.settings.storage_name); | |
} else if (app.settings.device_platform === "Android"){ | |
switch(type){ | |
case "string": | |
sharedpreferences.getString(key, app.storageSuccessHandler, app.storageErrorHandler); | |
break; | |
case "int": | |
sharedpreferences.getInt(key, app.storageSuccessHandler, app.storageErrorHandler); | |
break; | |
case "long": | |
sharedpreferences.getLong(key, app.storageSuccessHandler, app.storageErrorHandler); | |
break; | |
case "boolean": | |
sharedpreferences.getBoolean(key, app.storageSuccessHandler, app.storageErrorHandler); | |
break; | |
} | |
} else { | |
console.log("unsupported device"); | |
} | |
}, | |
storageRemovePassword: function(key) { | |
if (app.settings.device_platform === "iOS") { | |
new Keychain().removeForKey(app.storageErrorHandler, app.storageSuccessHandler, key, app.settings.storage_name); | |
} else if (app.settings.device_platform === "Android"){ | |
sharedpreferences.remove(key, app.storageSuccessHandler, app.storageErrorHandler); | |
} else { | |
console.log("unsupported device"); | |
} | |
}, | |
storageSuccessHandler: function(result) { | |
alert("SUCCESS: \r\n"+result); | |
}, | |
storageErrorHandler: function(result) { | |
alert("ERORR: \r\n"+result); | |
} | |
}; | |
app.initialize(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment