目录

Web Page Opening

API Declaration

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

{"name": "system.webview"}

Module Import

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")

Application Scope

Item

Description

Applicable devices

Mobile phones, tablets, and telematics devices

Applicable regions

Global

Definition

API

Description

webview.loadUrl(OBJECT)

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>).

webview.loadCustomTabsUrl(OBJECT)

Opens a web page using Custom Tabs and defines the appearance and style of HUAWEI Browser based on input parameters.

webview.preloadCustomTabsUrl(OBJECT)

Preloads the URL of a web page.

webview.getCustomTabsVersion()

Checks whether the Custom Tabs feature and relevant features are available on a mobile phone.

system.go(path)

Redirects to the specified page of the current app.

[h2]webview.loadUrl(OBJECT)

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
})

[h2]webview.loadCustomTabsUrl(OBJECT) (1078+, Not Formulated by the Vendor Alliance)

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.

  • true: yes
  • false: no

addDefaultShareMenuItem

boolean

No

Indicates whether to add the default sharing menu item.

  • true: yes
  • false: no

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:

  • COLOR_SCHEME_SYSTEM: system theme
  • COLOR_SCHEME_LIGHT: light theme
  • COLOR_SCHEME_DARK: dark theme

enableUrlBarHiding

boolean

No

Indicates whether to hide the address bar when a web page is dragged.

  • true: yes
  • false: no

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'
  },
})

[h2]webview.preloadCustomTabsUrl(OBJECT) (1078+, Not Formulated by the Vendor Alliance)

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
    })
  }
})

[h2]webview.getCustomTabsVersion() (1078+, Not Formulated by the Vendor Alliance)

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.

  • When the value is greater than or equal to 1, web pages can be opened using the Custom Tabs feature.
  • When the value is greater than or equal to 2, the user agent type can be used.

Sample code

const res = webview.getCustomTabsVersion()
prompt.showToast({
  message: 'CustomTabsVersion: ' + res.cctVersion
})

[h2]system.go(path)

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")

Demo

<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 Change History

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.

Links

[h2]FAQs

[h2]Cases

Web Page Opened By Calling the webview API Is Automatically Zoomed In On