Add the following configuration to the features attribute in the manifest.json file:
{"name": "system.webview"} Add the following configuration to the script element on the page where the API will be called:
import webview from '@system.webview'
Or
var webview = require("@system.webview") Item | Description |
|---|---|
Applicable devices | Mobile phones, tablets, and telematics devices |
Applicable regions | Global |
API | Description |
|---|---|
Opens a web page. The title bar style of this web page is the same as that of the WebView page to be displayed. Add content after WebView UserAgent. The format is hap/<Platform version number>/<Vendor ID><Platform app package name>/<Platform app version number><App package name>/<App version number> (<Source>). | |
Opens a web page using Custom Tabs and defines the appearance and style of HUAWEI Browser based on input parameters. | |
Preloads the URL of a web page. | |
Checks whether the Custom Tabs feature and relevant features are available on a mobile phone. | |
Redirects to the specified page of the current app. |
Description
Opens a web page. The title bar style of this web page is the same as that of the WebView page to be displayed. Add content after WebView UserAgent. The format is hap/<Platform version number>/<Vendor ID><Platform app package name>/<Platform app version number><App package name>/<App version number> (<Source>). The source here is the same as that in the result returned by the getInfo method of an app API. The WebView API supports file download.
For HTTPS websites, you are advised not to use any HTTP links on the page. Otherwise, man-in-the-middle attacks may occur. This is because quick apps do not differentiate HTTP and HTTPS scenarios at the underlying implementation layer.
OBJECT parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
url | string | Yes | URL of the web page to be loaded. CAUTION: The positioning function is only supported by HTTPS websites. |
allowthirdpartycookies (1030+) | boolean | No | Indicates whether cookies can be delivered across domains. The default value is false. |
showloadingdialog (1070+) | boolean | No | Indicates whether to display the loading progress bar. The default value is true. |
Sample code
webview.loadUrl({
url:'https://www.example.com',
allowthirdpartycookies: false
}) Description
Opens a web page using Custom Tabs and defines the appearance and style of HUAWEI Browser based on input parameters.
OBJECT parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
url | string | Yes | URL of the web page to be loaded. CAUTION: The positioning function is only supported by HTTPS websites. |
options | object | No | Parameters for customizing the appearance and style of HUAWEI Browser. |
success | function | No | Callback upon success. |
fail | function | No | Callback upon failure. |
complete | function | No | Callback upon completion. |
options parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
toolBarColor | string | No | Toolbar color, which is represented by a hexadecimal color code. |
showTitle | boolean | No | Indicates whether to display the title.
|
addDefaultShareMenuItem | boolean | No | Indicates whether to add the default sharing menu item.
|
navigationBarColor | string | No | Navigation bar color on a mobile phone, which is represented by a hexadecimal color code. |
colorScheme | string | No | Theme mode. The default value is COLOR_SCHEME_SYSTEM. The options are as follows:
|
enableUrlBarHiding | boolean | No | Indicates whether to hide the address bar when a web page is dragged.
|
isOpenAdFilter | boolean | No | Indicates whether to filter ads. The value false indicates that ad filtering is disabled. The setting of HUAWEI Browser is used by default. CAUTION: Ad filtering is disabled even if the parameter is set to true. |
isOpenBlockTrackingCookies | boolean | No | Indicates whether to block tracking cookies. The options are true (blocking enabled) and false (blocking disabled). The default value is false. However, the setting of HUAWEI Browser is used. |
whenAboutBlankClose | boolean | No | Indicates whether to close the custom tab page when the about:blank page is loaded. The options are true and false. The value true indicates that the custom tab page is closed when the about:blank page is loaded. |
userAgent | string | No | User agent type. The options are ANDROID_MODE, PC_MODE, IPHONE_MODE, and PAD_MODE. |
launchFlag (1078+) | string | No | Mode for using Custom Tabs of the browser. The value is a character string. If the value contains multiple modes, use commas (,) to separate them. Currently, only newTask is supported. newTask: Open Custom Tabs through a new task stack. This mode is applicable to the scenario where Custom Tabs is launched by a foreground service. For example, when a user taps a message in the notification bar, the music player is redirected to Custom Tabs. |
Sample code
webview.loadCustomTabsUrl({
url: 'https://www.toutiao.com',
options: {
toolBarColor: '#FF00FF',
showTitle: 'false',
addDefaultShareMenuItem: true,
navigationBarColor: '#00BFFF',
enableUrlBarHiding: 'true',
isOpenAdFilter: false,
isOpenBlockTrackingCookies: true,
whenAboutBlankClose: true,
colorScheme: 'COLOR_SCHEME_DARK'
},
}) Description
Preloads the URL of a web page.
Parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
url | string | No | URL to be preloaded. CAUTION: The positioning function is only supported by HTTPS websites. |
success | function | No | Callback upon success. |
fail | function | No | Callback upon failure. |
complete | function | No | Callback upon completion. |
Sample code
webview.preloadCustomTabsUrl({
url: "https://www.huawei.com/",
success: function (data) {
prompt.showToast({
message: data
})
},
fail: function (data) {
prompt.showToast({
message: data
})
}
}) Description
Checks whether the Custom Tabs feature and relevant features are available on a mobile phone.
Returns
Parameter | Type | Description |
|---|---|---|
cctVersion | int | Custom Tabs feature version supported by a mobile phone. The default value is 1.
|
Sample code
const res = webview.getCustomTabsVersion()
prompt.showToast({
message: 'CustomTabsVersion: ' + res.cctVersion
}) Description
Redirects a user from a web page opened by the WebView to the specified page of the current app.
Parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
path | string | Yes | Page to be displayed, for example, /detail?param1=value1. If the value of path is /, the page whose path is / is displayed. If no page is available, the home page is displayed. |
Sample code
system.go("/detail?param1=value1") <template>
<div>
<div>
<text>{{componentName}}</text>
</div>
<div>
<input type="button" onclick="loadUrl" value="Open the Huawei official website." />
</div>
<div>
<input type="button" onclick="loaddownLoadUrl" value="Open the web page with a download link." />
</div>
<div>
<input type="button" onclick="loadFileUploadSupportUrl" value="Open the WebView to support file upload." />
</div>
<div>
<input type="button" onclick="allowthirdcookie" value="Support passing cross-domain cookies." />
</div>
</div>
</template>
<style>
@import '../Common/css/common.css';
.item-container {
margin-bottom: 50px;
margin-right: 60px;
margin-left: 60px;
flex-direction: column;
}
</style>
<script>
import webview from '@system.webview'
export default {
data: {
componentName: 'webview'
},
onInit: function () {
this.$page.setTitleBar({ text: 'webview' })
},
loadUrl: function () {
webview.loadUrl({
url: 'https://www.huawei.com/'
});
},
loaddownLoadUrl: function () {
webview.loadUrl({
url: 'https://www.toutiao.com'
});
},
loadFileUploadSupportUrl() {
webview.loadUrl({
url: "https://lfmemberdev.hwcloudtest.cn:9094//fastapp/download/webview/file_chooser_demo_3.html"
});
},
allowthirdcookie: function () {
webview.loadUrl({
url: 'https://page.cainiao.com/mcn/publichome/pkg.html',
// url: "https://www.hao123.com/",
allowthirdpartycookies: true
});
}
}
</script> Version | Date | Description |
|---|---|---|
1070 | 2020-05-19 | Added the showloadingdialog parameter to loadUrl to determine whether to display the loading progress bar. |
1040 | 2019-06-24 | Added the demo. |
1030 | 2018-10-31 | Added the allowthirdpartycookies parameter to webview.loadUrl to specify whether cookies can be passed across domains. |
1020 | 2018-06-20 | Added the file download function. You can tap the download link to download files to the Download folder on the SD card. |