# [storage]()
Storage模塊管理應用本地數據,用于應用數據的保存和讀取。應用本地數據與localStorage、sessionStorage的區別在于數據有效域不同,前者可在應用內跨域操作,數據存儲期是持久化的,并且沒有容量限制。通過plus.storage可獲取應用本地數據管理對象。
### 方法:
- [getLength](http://www.dcloud.io/docs/api/zh_cn/storage.shtml#plus.storage.getLength): 獲取storage中保存的鍵值對的數量
- [getItem](http://www.dcloud.io/docs/api/zh_cn/storage.shtml#plus.storage.getItem): 通過key值檢索鍵值
- [setItem](http://www.dcloud.io/docs/api/zh_cn/storage.shtml#plus.storage.setItem): 存儲key-value
- [removeItem](http://www.dcloud.io/docs/api/zh_cn/storage.shtml#plus.storage.removeItem): 通過key值刪除鍵值對
- [clear](http://www.dcloud.io/docs/api/zh_cn/storage.shtml#plus.storage.clear): 清除應用所有的鍵值對
- [key](http://www.dcloud.io/docs/api/zh_cn/storage.shtml#plus.storage.key): 獲取鍵值對中指定索引值的key值
### 權限:
permissions
~~~
"Storage": {
"description": "訪問應用本地存儲數據"
}
~~~
# [getLength]()
獲取storage中保存的鍵值對的數量
~~~
var foo = plus.storage.getLength();
~~~
### 參數:
無
### 返回值:
Number : 鍵值對的數量
### 平臺支持:
- Android - 2.2+ (支持)
- iOS - 4.3+ (支持)
# [getItem]()
通過key值檢索鍵值
~~~
var foo = plus.storage.getItem(key);
~~~
### 參數:
- key: *( DOMString ) 必選 *
存儲的鍵值
### 返回值:
DOMString : 鍵對應的值,如果沒有保存則返回null。
### 平臺支持:
- Android - 2.2+ (支持)
- iOS - 4.3+ (支持)
# [setItem]()
存儲key-value
~~~
void plus.storage.setItem(key, value);
~~~
### 說明:
存儲的鍵和值沒有容量限制,但過多的數據量會導致效率降低,建議單個鍵值數據不要超過10Kb。
### 參數:
- key: *( DOMString ) 必選 *
存儲的鍵值
- value: *( DOMString ) 必選 *
存儲的內容
### 返回值:
void : 無
### 平臺支持:
- Android - 2.2+ (支持)
- iOS - 4.3+ (支持)
# [removeItem]()
通過key值刪除鍵值對
~~~
void plus.storage.removeItem(key);
~~~
### 參數:
- key: *( DOMString ) 必選 *
存儲的鍵值
### 返回值:
void : 無
### 平臺支持:
- Android - 2.2+ (支持)
- iOS - 4.3+ (支持)
# [clear]()
清除應用所有的鍵值對
~~~
void plus.storage.clear();
~~~
### 參數:
無
### 返回值:
void : 無
### 平臺支持:
- Android - 2.2+ (支持)
- iOS - 4.3+ (支持)
# [key]()
獲取鍵值對中指定索引值的key值
~~~
var foo = plus.storage.key(index);
~~~
### 參數:
- index: *( Number ) 必選 *存儲鍵值的索引
### 返回值:
void : 無
### 平臺支持:
- Android - 2.2+ (支持)
- iOS - 4.3+ (支持)
### 示例:
~~~
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<script type="text/javascript">
function loadAllKeyValue() {
var keyNames=[];
var values=[];
var numKeys=plus.storage.getLength();
for(var i=0; i<numKeys; i++) {
keyNames[i] = plus.storage.key(i);
values[i] = plus.storage.getItem(keyNames[i]);
}
}
</script>
</head>
<body>
</body>
</html>
~~~
- API參考
- Accelerometer
- Audio
- Camera
- Contacts
- Device
- Downloader
- Events
- Gallery
- Geolocation
- IO
- Key
- Messaging
- NativeUI
- Navigator
- Orientation
- Proximity
- SplashScreen
- Storage
- UI
- Uploader
- InterfaceOrientation
- Runtime
- WebView
- XMLHttpRequest
- Zip
- Plugins
- Barcode
- Maps
- Payment
- Push
- Share
- Speech
- Statistic
- Native.js
- Android
- iOS