WebView Component Reference

WebView is a component for displaying web pages, you could use this component to embed a mini web browser in your games. Because different platforms have different authorization, API and control methods for WebView component. And have not yet formed a unified standard, only Web, iOS, and Android platforms are currently supported.

webview

Click Add Component at the bottom of Properties panel and select WebView from UI Component to add the WebView component to the node.

For more information, please refer to the WebView API documentation.

WebView Properties

PropertiesFunction Explanation
URLA given URL to be loaded by the WebView, it should have a http or https prefix.
WebView EventsThe webview’s event callback, it will be triggered when certain webview event occurs.

Note: in the Node of the WebView Events property, you should fill in a Node that hangs the user script component, and in the user script you can use the relevant WebView event according to the user’s needs.

WebView Event

WebViewEvents Event

PropertyFunction Explanation
TargetNode with the script component.
ComponentScript component name.
HandlerSpecify a callback, when the WebView is loading the web pages, or the loading is finished or there are errors occurred. The callback will be called. For more information, please refer to Parameter of WebViewEvents.
CustomEventDataThe user specifies that any string is passed in as the last parameter of the event callback.

For more information, please refer to the Component.EventHandler Class documentation.

Parameter of WebViewEvents

NameFunction Explanation
LOADINGWebView is loading.
LOADEDWebView is finished loading.
ERRORErrors occurred when loading web pages.

For more information, please refer to the WebView Events documentation or 22.webview example of the test-cases-3d samples bundled with Creator.

Details Explanation

Currently, this component is only available on Web (Both PC and Mobile, iOS and Android (Not supported in the v2.0.0~2.0.6). It cannot be use on Mac or Windows which means if you preview WebView on these platforms, there is nothing to show.

Notes:

  1. This component doesn’t support load HTML file or execute JavaScript.
  2. If you don’t use WebView related features in your project, please ensure that the WebView module is removed from the Project -> Project Settings -> Module Config to help your game approval go as smoothly as possible on iOS App Store. If you really needs to use WebView (or the added third-party SDK comes with WebView), and therefore if the game is rejected by App Store, you can still try to appeal through email.

Add a callback via script

Method one

This method uses the same API that editor uses to add an event callback on Button component. You need to construct a Component.EventHandler object first, and then set the corresponding target, component, handler and customEventData parameters.

  1. import { _decorator, Component, WebView } from 'cc';
  2. const { ccclass, type } = _decorator;
  3. @ccclass('cc.MyComponent')
  4. export class MyComponent extends Component {
  5. @type(WebView)
  6. webview = null;
  7. start () {
  8. const eventHandler = new Component.EventHandler();
  9. eventHandler.target = newTarget; // This node is the one that the component that contains your event handler code belongs to
  10. eventHandler.component = "MyComponent";
  11. eventHandler.handler = "callback";
  12. eventHandler.customEventData = "foobar";
  13. this.webview.webviewEvents.push(eventHandler);
  14. }
  15. // Note that the order and type of parameters are fixed
  16. callback: function(webview, eventType, customEventData) {
  17. // here webview is a WebView component instance
  18. // here the value of eventType === WebView.EventType enum
  19. // The customEventData parameter here is equal to the "foobar"
  20. }
  21. }

Method two

Add event callback with webview.node.on (WebView.EventType.LOADED, ...)

  1. // Suppose we add event handling callbacks in the onLoad method of a component and perform event handling in the callback function:
  2. import { _decorator, Component, WebView } from 'cc';
  3. const { ccclass, type } = _decorator;
  4. @ccclass('WebViewCtrl')
  5. export class WebViewCtrl extends Component {
  6. @type(WebView)
  7. webview = null;
  8. start () {
  9. this.webview.node.on(WebView.EventType.LOADED, this.callback, this);
  10. }
  11. callback (event) {
  12. // The event here is an EventCustom object, and you can get the WebView component through event.detail
  13. let videoplayer = event.detail;
  14. // do whatever you want with webview
  15. // Also, note that this way the registered event can not pass customEventData
  16. }
  17. }

Likewise, you can also register WebView.EventType.LOADING, WebView.EventType.ERROR events, and the parameters of the callback function for these events are consistent with the WebView.EventType.LOADED parameters.

How to interact with WebView internal pages

Calling the WebView internal page

  1. import { _decorator, Component, WebView } from 'cc';
  2. const { ccclass, type } = _decorator;
  3. @ccclass('WebViewCtrl')
  4. export class WebViewCtrl extends Component {
  5. @type(WebView)
  6. webview = null;
  7. start () {
  8. // The Test here is a global function defined in your webView's internal page code
  9. this.webview.evaluateJS('Test()');
  10. }
  11. }

Note: cross-domain issues on Web platform need to be resolved by yourself as Cocos Creator does not assist with this.

WebView internal pages call external code

The current mechanism used by Android and iOS is to determine if the keywords in the URL prefix are the same, and if they are, then a callback is made.

  1. Setting the URL prefix keyword through setJavascriptInterfaceScheme
  2. The callback function is set by setOnJSCallback, and the function parameter is URL
  1. import { _decorator, Component, WebView } from 'cc';
  2. const { ccclass, type } = _decorator;
  3. @ccclass('WebViewCtrl')
  4. export class WebViewCtrl extends Component {
  5. @type(WebView)
  6. webview = null;
  7. // Setting in onLoad will make the callback useless, so we must set the WebView callback in the start cycle.
  8. start () {
  9. // Here are the keywords that are agreed with the internal page
  10. // Please set the scheme with lower case, the native won't identify the uppercase char scheme.
  11. let scheme = "testkey";
  12. function jsCallback (target, url) {
  13. // The return value here is the URL value of the internal page, and it needs to parse the data it needs.
  14. let str = url.replace(scheme + '://', ''); // str === 'a=1&b=2'
  15. // webview target
  16. console.log(target);
  17. }
  18. this.webview.setJavascriptInterfaceScheme(scheme);
  19. this.webview.setOnJSCallback(jsCallback);
  20. }
  21. }

When you need to interact with WebView through an internal page, you should set the internal page URL: testkey://(the data you want to callback to WebView later). WebView internal page code:

  1. <html>
  2. <body>
  3. <dev>
  4. <input type="button" value="Trigger" onclick="onClick()"/>
  5. </dev>
  6. </body>
  7. <script>
  8. function onClick () {
  9. // One of them sets up the URL scheme
  10. document.location = 'testkey://a=1&b=2';
  11. }
  12. </script>
  13. </html>

Due to limitations of the Web platform, it can not be implemented by this mechanism, but internal pages can interact in the following ways:

  1. <html>
  2. <body>
  3. <dev>
  4. <input type="button" value="Trigger" onclick="onClick()"/>
  5. </dev>
  6. </body>
  7. <script>
  8. function onClick () {
  9. // The parent here is actually the window of the WebView layer,
  10. // so that you can access the function defined in CC
  11. parent.cc.TestCode();
  12. // If TestCode is defined on window, then
  13. parent.TestCode();
  14. }
  15. </script>
  16. </html>

Note: cross-domain issues on Web platform need to be resolved by yourself as Cocos Creator does not assist with this.