We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
quickApp ReferencesQuick AppAPIFiles and DataData Exchange

Data Exchange

NOTICE

Data processed by the quick app APIs may be read by other apps when being used on a PC. Therefore, you are not advised to store sensitive data through those APIs.

API Declaration

Add the following configuration to the features attribute in the manifest.json file:

Collapse
Word wrap
Dark theme
Copy code
  1. {"name": "service.exchange"}

Module Import

Add the following configuration to the script element on the page where the API will be called:

Collapse
Word wrap
Dark theme
Copy code
  1. import exchange from '@service.exchange'

Or

Collapse
Word wrap
Dark theme
Copy code
  1. var exchange = require('@service.exchange')

Application Scope

Expand

Item

Description

Applicable devices

Mobile phones and tablets

Applicable regions

Global

Definition

Provides a method for data interaction between quick apps. A quick app can use this API to release data or obtain data from other quick apps. The data spaces for data exchange are as follows:

  • application: To read data in this space, you need to specify the package name and signature of the publisher and obtain authorization from the publisher.
  • global: To read data in this space, you do not need to specify the package name and signature of the publisher or obtain authorization from the publisher. This is because write operations of multiple apps may overwrite each other.
NOTICE

Global data supports only the set and get operations, not operations such as remove, clear, grantPermission, and revokePermission.

Expand

API

Description

exchange.get(OBJECT)

Reads data.

exchange.set(OBJECT)

Releases data to a quick app.

exchange.remove(OBJECT)

Deletes data from a quick app.

exchange.clear(OBJECT)

Clears data from a quick app.

exchange.grantPermission(OBJECT)

Assigns the data read permission to an app. By default, apps with the same signature as this app have the permission to read data.

exchange.revokePermission(OBJECT)

Revokes the data read permission from an app. The data read permission cannot be revoked from apps with the same signature as this app.

exchange.get(OBJECT)

Description

Reads data.

OBJECT

Expand

Parameter

Type

Mandatory

Description

package

string

No

Package name of the data publisher. This parameter is mandatory when scope is set to application but must be left empty when scope is set to global.

sign

string

No

Data publisher signature encrypted using SHA-256. This parameter is mandatory when scope is set to application but must be left empty when scope is set to global.

NOTE

This signature is specified in the signatureDigests parameter returned when the pkg.getSignatureDigests API is called successfully.

scope

string

No

Data publishing scope. The options are application and global. The default value is application.

key

string

Yes

Data key.

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion, regardless of whether the API call is successful or fails.

Parameters returned by success

Expand

Parameter

Type

Description

value

string

Data value.

Result codes returned by fail

Expand

Result Code

Description

202

Invalid parameter.

1000

No permission.

Sample code

Collapse
Word wrap
Dark theme
Copy code
  1. exchange.get({
  2. package: 'com.example',
  3. sign: '7a12ec1d66233f20a20141035b1f7937',
  4. key: 'token',
  5. success: function(ret) {
  6. console.log(`handling success, value = ${ret.value}`)
  7. },
  8. fail: function(data, code) {
  9. console.log(`handling fail, code = ${code}`)
  10. }
  11. })

exchange.set(OBJECT)

Description

Releases data to a quick app.

OBJECT

Expand

Parameter

Type

Mandatory

Description

key

string

Yes

Data key.

value

string

Yes

Data value.

scope

string

No

Data publishing scope. The options are application and global. The default value is application.

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion, regardless of whether the API call is successful or fails.

package (1080+)

string

No

Package name of the data publisher. This parameter is mandatory when scope is set to application and data needs to be modified for other apps. If scope is set to global, this parameter should be empty.

sign (1080+)

string

No

SHA-256 signed by the data publisher. This parameter is mandatory when scope is set to application and data needs to be modified for other apps. If scope is set to global, this parameter should be empty.

Result codes returned by fail

Expand

Result Code

Description

202

Invalid parameter.

Sample code

Collapse
Word wrap
Dark theme
Copy code
  1. exchange.set({
  2. package: 'com.fastapp.sample',
  3. sign: '6d3e6a3dcbdadb0aa94f64e33a36b01254cb6ef7e14be282d885cd95aeb952e0',
  4. key: 'token',
  5. value: '12347979',
  6. success: function() {
  7. console.log(`handling success`)
  8. },
  9. fail: function(data, code) {
  10. console.log(`handling fail, code = ${code}`)
  11. }
  12. })

exchange.remove(OBJECT)

Description

Deletes data from a quick app.

OBJECT

Expand

Parameter

Type

Mandatory

Description

key

string

Yes

Data key.

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion, regardless of whether the API call is successful or fails.

package (1080+)

string

No

Package name of the data publisher. This parameter is mandatory when data is deleted from other apps.

sign(1080+)

string

No

SHA-256 signed by the data publisher. This parameter is mandatory when data is deleted from other apps.

Sample code

Collapse
Word wrap
Dark theme
Copy code
  1. exchange.remove({
  2. package: 'com.fastapp.sample',
  3. sign: '6d3e6a3dcbdadb0aa94f64e33a36b01254cb6ef7e14be282d885cd95aeb952e0',
  4. key: 'token',
  5. success: function(ret) {
  6. console.log(`handling success, value = ${ret.value}`)
  7. },
  8. fail: function(data, code) {
  9. console.log(`handling fail, code = ${code}`)
  10. }
  11. })

exchange.clear(OBJECT)

Description

Clears data from a quick app.

OBJECT

Expand

Parameter

Type

Mandatory

Description

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion, regardless of whether the API call is successful or fails.

Sample code

Collapse
Word wrap
Dark theme
Copy code
  1. exchange.clear({
  2. success: function() {
  3. console.log(`handling success`)
  4. },
  5. fail: function(data, code) {
  6. console.log(`handling fail, code = ${code}`)
  7. }
  8. })

exchange.grantPermission(OBJECT)

Description

Assigns the data read permission to an app. By default, apps with the same signature as this app have the permission to read data.

OBJECT

Expand

Parameter

Type

Mandatory

Description

package

string

Yes

Package name of the app to be authorized.

sign

string

Yes

Signature (SHA-256) of the app to be authorized.

key

string

No

Data key. If this parameter is left empty, the data read permission on all keys is assigned.

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion, regardless of whether the API call is successful or fails.

writable(1080+)

boolean

No

Indicates whether data can be modified or deleted by an authorized app. The default value is false.

  • If the value is empty or false, only the read permission is granted, and the modify or delete permission will be revoked.
  • If the value is true, the read, write, and delete permissions are granted at the same time. To cancel the read permission, call the exchange.revokePermission method.

Result codes returned by fail

Expand

Result Code

Description

202

Invalid parameter.

Sample code

Collapse
Word wrap
Dark theme
Copy code
  1. exchange.grantPermission({
  2. package: 'com.fastapp.sample',
  3. sign: '6d3e6a3dcbdadb0aa94f64e33a36b01254cb6ef7e14be282d885cd95aeb952e0',
  4. writable: true,
  5. success: function() {
  6. console.log(`handling success`)
  7. },
  8. fail: function(data, code) {
  9. console.log(`handling fail, code = ${code}`)
  10. }
  11. })

exchange.revokePermission(OBJECT)

Description

Revokes the data read permission from an app. The data read permission cannot be revoked from apps with the same signature as this app.

OBJECT

Expand

Parameter

Type

Mandatory

Description

package

string

Yes

Package name of the app whose data read permission is to be revoked.

key

string

No

Data key. If this parameter is left empty, data read permission on all keys is revoked.

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion, regardless of whether the API call is successful or fails.

Result codes returned by fail

Expand

Result Code

Description

202

Invalid parameter.

Sample code

Collapse
Word wrap
Dark theme
Copy code
  1. exchange.revokePermission({
  2. package: 'com.example',
  3. success: function() {
  4. console.log(`handling success`)
  5. },
  6. fail: function(data, code) {
  7. console.log(`handling fail, code = ${code}`)
  8. }
  9. })

Demo

Collapse
Word wrap
Dark theme
Copy code
  1. <template>
  2. <div class="container">
  3. <div class="item-container">
  4. <div class="item-content">
  5. <div class="item-content-detail">
  6. <text class="txt">key:</text>
  7. <input class="input" placeholder=" please input key" value={{exchangeKey}} onchange="keyFn" />
  8. </div>
  9. <div class="item-content-detail">
  10. <text class="txt">value:</text>
  11. <input class="input" placeholder="please input value" value={{exchangeValue}} onchange="valueFn" />
  12. </div>
  13. </div>
  14. <!-- Set data -->
  15. <input type="button" class="btn" onclick="setAppExchange" value="setAppExchange" />
  16. <input type="button" class="btn" onclick="setGlobalExchange" value="setGlobalExchange" />
  17. <!-- Read data -->
  18. <input type="button" class="btn" onclick="getAppExchange" value="getAppExchange key:{{exchangeKey}}" />
  19. <input type="button" class="btn" onclick="getGlobalExchange" value="getGlobalExchange key:{{exchangeKey}}" />>
  20. <!-- To grant authorization -->
  21. <input type="button" class="btn" onclick="grantPermission" value="grantPermission" />
  22. <input type="button" class="btn" onclick="revokePermission" value="revokePermission" />
  23. <!-- Delete operation -->
  24. <input type="button" class="btn" onclick="clearExchange" value="clearExchange" />
  25. <input type="button" class="btn" onclick="removeExchange" value="removeExchange" />
  26. </div>
  27. </div>
  28. </template>
  29. <style>
  30. .container{
  31. flex: 1;
  32. flex-direction: column;
  33. }
  34. .item-container {
  35. margin-bottom: 50px;
  36. margin-right: 60px;
  37. margin-left: 60px;
  38. flex-direction: column;
  39. }
  40. .item-content {
  41. flex-direction: column;
  42. background-color: #ffffff;
  43. padding-left: 30px;
  44. padding-right: 30px;
  45. padding-top: 15px;
  46. padding-bottom: 15px;
  47. margin-bottom: 100px;
  48. }
  49. .item-content-detail {
  50. align-items: center;
  51. }
  52. .input {
  53. flex: 1;
  54. font-size: 30px;
  55. padding-left: 20px;
  56. }
  57. .txt {
  58. width: 100px;
  59. padding-top: 15px;
  60. padding-bottom: 15px;
  61. text-align: right;
  62. }
  63. .btn {
  64. height: 80px;
  65. text-align: center;
  66. border-radius: 5px;
  67. margin-right: 60px;
  68. margin-left: 60px;
  69. margin-bottom: 50px;
  70. color: #ffffff;
  71. font-size: 30px;
  72. background-color: #0faeff;
  73. }
  74. </style>
  75. <script>
  76. import exchange from '@service.exchange'
  77. import prompt from '@system.prompt'
  78. export default {
  79. data: {
  80. exchangeKey: '',
  81. exchangeValue: '',
  82. },
  83. onInit: function () {
  84. this.$page.setTitleBar({ text: 'exchange' })
  85. },
  86. keyFn: function (e) {
  87. this.exchangeKey = e.text
  88. },
  89. valueFn: function (e) {
  90. this.exchangeValue = e.text
  91. },
  92. setAppExchange: function () {
  93. var that = this
  94. if (this.exchangeKey && this.exchangeValue) {
  95. exchange.set({
  96. key: this.exchangeKey,
  97. value: this.exchangeValue,
  98. scope: 'application',
  99. success: function (ret) {
  100. prompt.showToast({
  101. message: '{key:' + that.exchangeKey + ',value:' + that.exchangeValue + '}',
  102. })
  103. console.log("message"+ that.exchangeKey + ',value:' + that.exchangeValue )
  104. },
  105. fail: function (erromsg, errocode) {
  106. prompt.showToast({ message: 'set fail --- ' + errocode + ':' + erromsg })
  107. console.info('set fail --- ' + errocode + ':' + erromsg)
  108. },
  109. complete: function () {
  110. prompt.showToast({ message: 'set complete' })
  111. console.info('set complete ')
  112. }
  113. })
  114. } else {
  115. prompt.showToast({
  116. message: 'Please enter the key and value values'
  117. })
  118. }
  119. },
  120. setGlobalExchange: function () {
  121. var that = this
  122. if (this.exchangeKey && this.exchangeValue) {
  123. exchange.set({
  124. key: this.exchangeKey,
  125. value: this.exchangeValue,
  126. scope: 'global',
  127. success: function (ret) {
  128. prompt.showToast({
  129. message: '{key:' + that.exchangeKey + ',value:' + that.exchangeValue + '}',
  130. })
  131. },
  132. fail: function (erromsg, errocode) {
  133. prompt.showToast({ message: 'set fail --- ' + errocode + ':' + erromsg })
  134. console.info('set fail --- ' + errocode + ':' + erromsg)
  135. },
  136. complete: function () {
  137. prompt.showToast({ message: 'set complete' })
  138. console.info('set complete ')
  139. }
  140. })
  141. } else {
  142. prompt.showToast({
  143. message: 'Please enter the key and value values'
  144. })
  145. }
  146. },
  147. getAppExchange: function () {
  148. exchange.get({
  149. package: 'com.fastapp.sample.global',
  150. scope: 'application',
  151. sign: '6d3e6a3dcbdadb0aa94f64e33a36b01254cb6ef7e14be282d885cd95aeb952e0',
  152. key: this.exchangeKey,
  153. success: function (ret) {
  154. console.info('exchange.get(): ', JSON.stringify(ret))
  155. prompt.showToast({
  156. message: 'value: ' + ret,
  157. })
  158. },
  159. fail: function (erromsg, errocode) {
  160. prompt.showToast({ message: 'get fail --- ' + errocode + ':' + erromsg })
  161. console.info('get fail --- ' + errocode + ':' + erromsg)
  162. },
  163. complete: function () {
  164. console.info('get complete ')
  165. }
  166. })
  167. },
  168. getGlobalExchange: function () {
  169. var that = this
  170. exchange.get({
  171. package: '',
  172. sign: '',
  173. scope: 'global',
  174. key: this.exchangeKey,
  175. success: function (ret) {
  176. console.info('storage.get(): ', JSON.stringify(ret))
  177. that.storageShow = ret
  178. prompt.showToast({
  179. message: 'value: ' + ret,
  180. })
  181. },
  182. fail: function (erromsg, errocode) {
  183. prompt.showToast({ message: 'get fail --- ' + errocode + ':' + erromsg })
  184. console.info('get fail --- ' + errocode + ':' + erromsg)
  185. },
  186. complete: function () {
  187. console.info('get complete ')
  188. }
  189. })
  190. },
  191. grantPermission: function () {
  192. exchange.grantPermission({
  193. package: 'com.exchang.demo',
  194. sign: '6f65a2a4fbb1941436f2bfeb23b1c0c4003f30de2b9c4d31a4d188b1de7d0893',
  195. key: this.exchangeKey,
  196. success: function (ret) {
  197. console.info('exchange.grantPermission(): ', JSON.stringify(ret))
  198. prompt.showToast({
  199. message: 'value: ' + ret,
  200. })
  201. },
  202. fail: function (erromsg, errocode) {
  203. prompt.showToast({ message: 'grantPermission fail --- ' + errocode + ':' + erromsg })
  204. console.info('grantPermission fail --- ' + errocode + ':' + erromsg)
  205. },
  206. complete: function () {
  207. console.info('grantPermission complete ')
  208. }
  209. })
  210. },
  211. revokePermission: function () {
  212. exchange.revokePermission({
  213. package: 'com.fastapp.sample.global',
  214. key: this.exchangeKey,
  215. success: function (ret) {
  216. console.info('exchange.revokePermission(): ', JSON.stringify(ret))
  217. prompt.showToast({
  218. message: 'value: ' + ret,
  219. })
  220. },
  221. fail: function (erromsg, errocode) {
  222. prompt.showToast({ message: 'revokePermission fail --- ' + errocode + ':' + erromsg })
  223. console.info('revokePermission fail --- ' + errocode + ':' + erromsg)
  224. },
  225. complete: function () {
  226. console.info('revokePermission complete ')
  227. }
  228. })
  229. },
  230. clearExchange: function () {
  231. exchange.clear({
  232. success: function () {
  233. prompt.showToast({
  234. message: 'success',
  235. })
  236. },
  237. fail: function (erromsg, errocode) {
  238. prompt.showToast({ message: 'clear fail --- ' + errocode + ':' + erromsg })
  239. console.info('clear fail --- ' + errocode + ':' + erromsg)
  240. },
  241. complete: function () {
  242. prompt.showToast({ message: 'clear complete' })
  243. console.info('clear complete ')
  244. }
  245. })
  246. },
  247. removeExchange: function () {
  248. if (this.exchangeKey) {
  249. exchange.remove({
  250. key: this.exchangeKey,
  251. success: function () {
  252. prompt.showToast({
  253. message: 'success',
  254. })
  255. },
  256. fail: function (erromsg, errocode) {
  257. prompt.showToast({ message: 'get fail --- ' + errocode + ':' + erromsg })
  258. console.info('get fail --- ' + errocode + ':' + erromsg)
  259. },
  260. complete: function () {
  261. console.info('get complete ')
  262. }
  263. })
  264. } else {
  265. prompt.showToast({ message: 'key is null' })
  266. }
  267. }
  268. }
  269. </script>

The following figure shows the final effect.

Version Change History

Expand

Version

Date

Description

1080

2021-12-28

Added the function of allowing authorized apps to write and delete data through the exchange APIs.

1050

2019-09-20

First official release.

Search in References
Enter a keyword.