Call the setAPDataStorage interface to save a string to the unified client storage. The string length cannot exceed 200 × 1024.
The underlying storage service component is implemented differently in iOS and Android. The unified storage component for Android does not support the
type=userproperty. To maintain consistency with the frontend interface, if you settype=user, the Android underlying layer modifies the key tokey=key + "_" +MD5(userId + userId + userId)before storing the data. The client must construct the key in the same way to retrieve the data.In baselines 10.1.60 and earlier, you must configure the client to allow the interface to retrieve the userId. Otherwise, the storage interface cannot store data separately for each
userId. For more information, see Implement the H5LoginProvider interface.In baselines 10.1.68 and later, the
userIddefaults to the value set byMPLogger.setUserId. IfH5LoginProvideris implemented, its value is used instead.
Implement the H5LoginProvider interface
Android
You can implement the H5LoginProvider interface and set the instance class in H5ProviderManager.
Code example
package com.mpaas.nebula.provider;
import android.os.Bundle;
import com.alipay.mobile.common.logging.api.LoggerFactory;
import com.alipay.mobile.nebula.provider.H5LoginProvider;
public class H5LoginProviderImpl implements H5LoginProvider {
// Omit other code
@Override
public String getUserId() {
// This method returns the userId
return LoggerFactory.getLogContext().getUserId();
}
// Omit other code
}Set H5LoginProvider
H5Utils.setProvider(H5LoginProvider.class.getName(), new H5LoginProviderImpl());How to use the setAPDataStorage interface
AlipayJSBridge.call('setAPDataStorage', {
type: "common",
business: "customBusinessKey",
key: "customKey",
value: "customValue"
}, function(result) {
alert(JSON.stringify(result));
});Code example
<button id="J_saveDataBtn" class="btn">Save data</button>
<button id="J_getDataBtn" class="btn">View data</button>
<button id="J_removeDataBtn" class="btn">Delete data</button>
<script>
function ready(callback) {
// If jsbridge is already injected, call it directly.
if (window.AlipayJSBridge) {
callback && callback();
} else {
// If not, listen for the injection event.
document.addEventListener('AlipayJSBridgeReady', callback, false);
}
}
ready(function() {
document.querySelector('#J_saveDataBtn').addEventListener('click', function(e) {
AlipayJSBridge.call('setAPDataStorage', {
type: "common",
business: "customBusinessKey",
key: "customKey",
value: "customValue"
}, function(result) {
alert(JSON.stringify(result));
});
}, false);
document.querySelector('#J_getDataBtn').addEventListener('click', function(e) {
AlipayJSBridge.call('getAPDataStorage', {
type: "common",
business: "customBusinessKey",
key: "customKey"
}, function(result) {
alert(JSON.stringify(result));
});
}, false);
document.querySelector('#J_removeDataBtn').addEventListener('click', function(e) {
AlipayJSBridge.call('removeAPDataStorage', {
type: "common",
business: "customBusinessKey",
key: "customKey"
}, function(result) {
alert(JSON.stringify(result));
});
}, false);
}, false);
</script>API reference
AlipayJSBridge.call('setAPDataStorage', {
type, business, key, value
});Request parameters
Property | Type | Description | Required | Default value |
type | String | Specifies whether to store data by user dimension ( | N | "common" |
business | String | A custom business identifier. This must match the identifier used in the corresponding client code. In Android, this business identifier corresponds to the | No | "NebulaBiz" |
key | String | The key for the custom data. | Yes | "" |
value | String | The value to store. Only strings are supported. JSON-formatted data must be converted to a string first. | Yes | "" |
Response parameters
The callback function receives a result: {success} parameter.
Property | Type | Description |
success | bool | Indicates whether the data was saved successfully. |
Error codes
Error code | Description |
11 | The string length exceeds the limit. |