Saturday, April 19, 2025
spot_img

WebView Not Working in Cocos Creator 3.8.2 – setOnJSCallback & setJavascriptInterfaceScheme Not Triggering – Cocos Creator


Good day everybody,

I’m utilizing Cocos Creator 3.8.2 and dealing with points with the WebView part. The next features don’t appear to work:

  1. setOnJSCallback(callback) – This doesn’t set off when a JavaScript occasion is distributed from the WebView.
  2. setJavascriptInterfaceScheme(scheme) – Even after setting a customized scheme, it doesn’t register correctly, and any try and set off it leads to an error:
Didn't launch '://' as a result of the scheme doesn't have a registered handler.

Has anybody efficiently used setOnJSCallback and setJavascriptInterfaceScheme in Cocos Creator 3.8.2?
Is there an alternate method, or is that this a recognized difficulty?

Any steerage can be appreciated!
@Tom_k

You may comply with this tutorial: Tutorial: Interplay and hierarchy of WebView inside pages
I’ve applied this mechanism efficiently based mostly on it

By following this tutorial, WebView is working nice within the browser however not working correctly on cell units in Cocos Creator 3.8.2, though it was working in Cocos Creator 2.4.7.

Would you thoughts posting your webview code right here?
Final time I bumped into this drawback was as a result of my scheme identify contained uppercase letters.

//Code of WebViewController for cocos 3.8.2

@property(cc.WebView)
  webview: cc.WebView = null;
  scheme: string = "rgmrwebcall";

onLoad() {
 
    WebViewController.occasion = this;

       this.node.energetic = false;
  }

openWebView(url: string){
NetworkManager.getInstance().community.node.on(
      "closewebview",
      this.OnCloseWebView,
      this
    );
    this.urlToCall = url;
    var self = this;
    operate jsCallback(goal, urlReceived) {
      // The return worth right here is the URL worth of the interior
      // web page, you might want to parse the information you want.
      Logs.print("jsCallback "+urlReceived)
      var dataEncoded = urlReceived.change(self.scheme + "://", ""); // str === 'a=1&b=2'
      let dataDecoded: string = decodeURIComponent(dataEncoded);
      let deeplinkArrayStr: string = dataDecoded.change("knowledge=", "");
      this.OnCloseWebView();
      // Code to shut the webview
     }

          this.webview.setJavascriptInterfaceScheme(this.scheme);
              this.webview.setOnJSCallback(jsCallback);
}


onWebFinishLoad(sender, occasion) {
  attempt {
    Logs.print("onWebFinishLoad");
    let webview: cc.WebView = this.getComponentInChildren(cc.WebView);

    var loadStatus = "";
    if (occasion === cc.WebView.EventType.LOADED) {
      // if(!cc.sys.isBrowser)
        if(!NetworkManager.getInstance().community.IsVariantAAR())
      {
      loadStatus = " is loaded!";

      this.urlToCall = NetworkManager.getInstance().community.replaceDataInAPiCalls(
        this.urlToCall
      );
      Logs.print(" first net 1  " + this.urlToCall);
webview.evaluateJS(
        'Open("' +
          this.urlToCall +
          '","' +
          cc.sys.localStorage.getItem(GameConst.userLoginToken) +
          '")'
      );
this.node.width = 576;
            this.node.peak = 1024;
     
    } else if (occasion === cc.WebView.EventType.LOADING) {
      loadStatus = " is loading!";
    } else if (occasion === cc.WebView.EventType.ERROR) {
      loadStatus = " load error!";
      // this.webview.node.energetic=false;
      this.node.energetic = false;
}
}

OnCloseWebView() {
      NetworkManager.getInstance().community.node.off(
        "closewebview",
        this.OnCloseWebView,
        this
      );
      this.node.destroy();
}

//In logs: occasion is throwing error on click on of the button

Are you migrating code from Cocos Creator 2.4+ ? in Cocos Creator 3.0+, namespace cc now not exists

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest Articles