Cordova-plugin-secure-storage 使用教程
1. 项目的目录结构及介绍
cordova-plugin-secure-storage/
├── src/
│ ├── android/
│ ├── ios/
│ ├── blackberry10/
│ ├── wp8/
│ └── windows/
├── www/
│ └── SecureStorage.js
├── plugin.xml
├── package.json
└── README.md
- src/: 包含各个平台的实现代码。
- android/: Android 平台的实现代码。
- ios/: iOS 平台的实现代码。
- blackberry10/: BlackBerry 10 平台的实现代码。
- wp8/: Windows Phone 8 平台的实现代码。
- windows/: Windows 平台的实现代码。
- www/: 包含插件的 JavaScript 接口文件
SecureStorage.js
。 - plugin.xml: 插件的配置文件,定义了插件的名称、版本、依赖等信息。
- package.json: 插件的 npm 包配置文件,包含版本、依赖等信息。
- README.md: 项目的说明文档。
2. 项目的启动文件介绍
项目的启动文件主要是 www/SecureStorage.js
,这个文件定义了插件的 JavaScript 接口。以下是 SecureStorage.js
的主要内容:
var exec = require('cordova/exec');
var SecureStorage = function() {};
SecureStorage.prototype.get = function(successCallback, errorCallback, service, key) {
exec(successCallback, errorCallback, "SecureStorage", "get", [service, key]);
};
SecureStorage.prototype.set = function(successCallback, errorCallback, service, key, value) {
exec(successCallback, errorCallback, "SecureStorage", "set", [service, key, value]);
};
SecureStorage.prototype.remove = function(successCallback, errorCallback, service, key) {
exec(successCallback, errorCallback, "SecureStorage", "remove", [service, key]);
};
module.exports = new SecureStorage();
- get: 获取存储的值。
- set: 设置存储的值。
- remove: 删除存储的值。
3. 项目的配置文件介绍
plugin.xml
plugin.xml
是 Cordova 插件的配置文件,定义了插件的名称、版本、依赖等信息。以下是 plugin.xml
的主要内容:
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="cordova-plugin-secure-storage"
version="3.0.1">
<name>SecureStorage</name>
<js-module name="SecureStorage" src="www/SecureStorage.js">
<clobbers target="SecureStorage" />
</js-module>
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="SecureStorage">
<param name="android-package" value="com.crypho.plugins.SecureStorage"/>
</feature>
</config-file>
<source-file src="src/android/SecureStorage.java" target-dir="src/com/crypho/plugins"/>
</platform>
<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="SecureStorage">
<param name="ios-package" value="SecureStorage"/>
</feature>
</config-file>
<header-file src="src/ios/SecureStorage.h"/>
<source-file src="src/ios/SecureStorage.m"/>
</platform>
<!-- 其他平台的配置 -->
</plugin>
- id: 插件的唯一标识符。
- version: 插件的版本号。
- js-module: 定义 JavaScript 模块。
- platform: 定义各个平台的配置。
package.json
package.json
是 npm 包的配置文件,包含版本、依赖等信息。以下是 package.json
的主要内容:
{
"name": "cordova-