SDK APIs
Modern SDK tools to help you access imToken's public APIs.
Introduction
With the @consenlabs-fe/webview
, you can easily access and use the various interfaces built into the imToken WebView environment. Whether you're migrating a DApp to imToken or adapting a Web application, you can quickly solve it with minimal code and minimal invasiveness.
The SDK includes functions to obtain the language environment, common native UI, route navigation, scan QR codes, etc. The SDK itself does not contain dependencies and is only used as a tool to bond the host environment and will not cause any invasive damage to your project.
Features
Complete TypeScript support
All interfaces and methods have full type support.
Multi-module support
In modern front-end projects, the esm
mode is used by default, but we also provide cjs
umd
and ways.
Promise
Based on v1, all our asynchronous interfaces support promise by default.
Community
We've added support for the community, where you can create a discussion for any questions about the API or tools.
Getting started
If you are looking for a complete project example, please check out the sample applications we have prepared:
React project sample: examples/react-app
Vue project sample: examples/vue-app
UMD project sample: examples/umd
Install
You can use the package management tool to install @consenlabs-fe/webview
, this is the recommended way. For this, your project may need the NodeJS environment with the NPM management tool. (or Yarn)
NPM: (recommended installation)
Yarn:
CDN:
@consenlabs-fe/webview
is available on unpkg and jsdelivr. (the CDN may require some time synchronization, please note the version you are using)
Import and usage
Before you can use it, you need to import
the module:
As a suggestion to maintain compatibility, we recommend that you always check the current environment first:
API References
All API interfaces, types, and examples are listed here. In fact, if you are using TypeScript, you can already get good type hints in your project.
General
This is the API that comes with the root module and is usually used to check the environment and the processing of generic scenarios.
isTokenEnv
Check if the current context is imToken WebView and return a boolean value.
Types
Usage Example
getVersion
Get the current version of the imToken App.
Types
Usage Example
isGreaterThanOrEqualVersion
The current App version is greater than or equal to the specified value.
Types
Usage Example
compareSemver
Compare the two versions of the value and return the number.
Requires that the comparison value must be a semantic version of the string. The return value rules are the same as for the JavaScript sort function.
Types
Usage Example
isCancelError
Check if the current error was caused by a user cancellation.
Types
Usage Example
ERRORS
Error constants. The source of the error message, most of the time you do not need to compare this constant.
Types
apis
API for communicating with the host environment, please see below for details.
device API
getCurrentLanguage
Get the current language environment, which usually comes from the user's settings within the imToken application.
Types
Usage Example
getCurrentCurrency
Get the current currency, returns the currency string.
Types
Usage Example
native API
Provide native platform UI and interaction capabilities.
alert
Display the string as an alert box.
Types
Usage Example
confirm
Display the string as a confirm box.
A user clicks on the confirm or cancel button action will be returned as a Boolean value.
Types
Usage Example
setLoading
Set the entire WebView to the loading state.
If you do not actively cancel this status, users will not be able to operate. The loaded state will block all interactive events.
Types
Usage Example
share
Share a link or picture.
If the shared content is an image, it needs to be pre-converted to base64
format.
The success of the sharing depends on the user's confirmation and whether the user allows permissions.
Types
Usage Example
scanQRCode
Evoke the camera to scan the QR code, if the scan is successful, the parsed string will be returned.
Types
Usage Example
setClipboard
Filling the user's clipboard with strings.
Types
Usage Example
navigator API
Routing navigation and settings page.
closeDapp
Close the current DApp.
Types
Usage Example
goBack
Returns the previous level of routing, or closes the app if there is no routing stack.
Types
Usage Example
routeTo
Navigate to a specific screen.
Types
Usage Example
getOrientation
Get the direction of the current device.
Types
Usage Example
setOrientation
Set the orientation of the current device, which can be very helpful when playing multimedia messages.
Types
Usage Example
setTitle
Dynamically set DApp title. No effect to document.title
.
Types
Usage Example
user API
showAccountSwitch
Show switchable user wallets. If the switch is successful, the new address will be returned.
Types
Usage Example
layout API
setOptions
Set the frame layout style of the WebView.
This updates the layout of the current WebView synchronously and can only be applied after the page has been loaded and parsed. If you want to make changes to the layout while the page is loading, please refer to our preload configuration documentation.
Types
Usage Example
internal API
Unstable API, mainly for advanced user custom development and debugging.
Last updated