目录

ArkWeb页面跳转常见问题

Q:Web组件是否支持通过URL Scheme协议跳转其它App?

A:Web组件支持通过URL Scheme跳转到其它App。开发者可以通过Web组件的onLoadIntercept回调拦截默认跳转逻辑,并在其中使用Deep Linking或App Linking的方式自定义跳转逻辑完成应用跳转。

参考链接:使用Deep Linking实现应用间跳转、使用App Linking实现应用间跳转。

Q:如何在H5页面中监听侧滑手势,实现H5页面逐级后退功能?

A:针对在H5页面中监听侧滑手势,来实现页面回退功能,根据Web组件所在层级不同方案分别如下:

  1. 如果是在@Entry装饰的组件中,可以直接通过onBackPress拦截侧滑事件,然后通过WebView的forward()和backward()接口向前/向后浏览上一个/下一个历史记录,实现页面的前进和后退。示例代码如下:
    import { webview } from '@kit.ArkWeb';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    @Entry
    @Component
    struct Method1 {
      private webviewController: webview.WebviewController = new webview.WebviewController();
      private url: string = 'https://www.***.com'; // 需要替换为真实的url
    
      onBackPress(): boolean | void {
        try {
          const result = this.webviewController.accessBackward();
          if (result) {
            this.webviewController.backward();
            return true;
          }
          return false;
        } catch (error) {
          console.error(`catch exception: ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
          return false;
        }
      }
    
      build() {
        Column() {
          Web({
            src: this.url,
            controller: this.webviewController
          })
            .fileAccess(false)
            .geolocationAccess(false);
        }
        .height('100%')
        .width('100%');
      }
    }
  2. 在NavDestination组件中,采用onBackPressed回调对返回事件进行拦截,实现页面的前进和后退。示例代码如下:
    import { WebPage } from './WebPage';
    
    @Entry
    @Component
    struct Method2 {
      @Provide('NavPathStack') stack: NavPathStack = new NavPathStack();
    
      @Builder
      pageMap(name: string) {
        if (name === 'WebPage') {
          WebPage();
        }
      }
    
      build() {
        Column() {
          Navigation(this.stack) {
            Button('Navigation到Web页面')
              .onClick(() => {
                this.stack.pushPathByName('WebPage', null, false);
              });
          }
          .titleMode(NavigationTitleMode.Full)
          .mode(NavigationMode.Stack)
          .hideToolBar(true)
          .navDestination(this.pageMap);
        }
        .height('100%')
        .width('100%');
      }
    }
    import { webview } from '@kit.ArkWeb';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    @Component
    export struct WebPage {
      @Consume('NavPathStack') stack: NavPathStack;
      private webVC: webview.WebviewController = new webview.WebviewController();
      private url: string = 'https://www.***.com'; // 需要替换为真实的url
    
      build() {
        NavDestination() {
          Column() {
            Web({
              src: this.url,
              controller: this.webVC,
            })
              .fileAccess(false)
              .geolocationAccess(false)
              .javaScriptAccess(true)
              .domStorageAccess(true)
              .onlineImageAccess(true);
          };
        }
        .height('100%')
        .width('100%')
        .onBackPressed(() => {
          try {
            // 判断是否可以返回,如果可以的话调用backward()接口返回
            const result = this.webVC.accessBackward();
            if (result) {
              this.webVC.backward();
              return true;
            }
            return false;
          } catch (error) {
            console.error(`catch exception: ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
            return false;
          }
        });
      }
    }

Q:如何实现点击本地H5页面后返回应用侧页面?

A:如果要实现在H5页面中点击返回到应用侧,可以通过WebMessagePort进行消息的发送以及接收,H5页面接收到消息后调用应用侧方法,实现点击H5页面按钮页面后退。

  1. Web页面代码如下:
    import { webview } from '@kit.ArkWeb';
    import { common } from '@kit.AbilityKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    interface JSBridgeModel {
      method: string,
      name?: string,
      password?: string
      appId?: string
      shareType?: number
      newWebUrl?: string
      orientation?: number
    }
    
    @Component
    export struct WebPage {
      pageInfos: NavPathStack = new NavPathStack();
      controller: webview.WebviewController = new webview.WebviewController();
      ports: webview.WebMessagePort[] = [];
      context = this.getUIContext().getHostContext() as common.UIAbilityContext;
    
      onBackPress() {
        if (this.controller.accessBackward()) {
          this.controller.backward();
          return true;
        }
        return false;
      }
    
      handleJsMsg(model: JSBridgeModel) {
        if (model.method === 'back') {
          // 返回上一级页面
          this.pageInfos.pop();
        }
      }
    
      build() {
        NavDestination() {
          Column() {
            Web({ src: $rawfile('index.html'), controller: this.controller })
              .domStorageAccess(true) // 是否开启文档对象模型存储接口
              .imageAccess(true) // 是否允许自动加载图片资源,默认允许
              .javaScriptAccess(true) // 是否允许执行JavaScript
              .databaseAccess(true) // 是否开启数据库存储API权限,默认不开启
              .onlineImageAccess(true) // 是否允许从网络加载图片资源
              .fileAccess(false)
              .geolocationAccess(false)
              .onPageEnd(() => {
                try {
                  // 1、创建两个消息端口。
                  this.ports = this.controller.createWebMessagePorts();
                  // 2、在应用侧的消息端口(如端口1)上注册回调事件。
                  this.ports[1].onMessageEvent((result: webview.WebMessage) => {
                    if (typeof (result) === 'string') {
                      const resStr = result.toString();
                      // 处理js回调事件
                      const objData: JSBridgeModel = JSON.parse(resStr) as JSBridgeModel;
                      this.handleJsMsg(objData);
                    }
                  });
                  // 3、将另一个消息端口(如端口0)发送到HTML侧,由HTML侧保存并使用。
                  this.controller.postMessage('__init_port__', [this.ports[0]], '*');
                } catch (error) {
                  console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
                }
              });
          }
          .height('100%')
          .width('100%')
          .backgroundColor('#ace');
        }
        .hideBackButton(true)
        .onReady((context: NavDestinationContext) => {
          this.pageInfos = context.pathStack;
        });
      }
    }
  2. 应用侧页面通过路由跳转Web页面:
    import { WebPage } from './WebPage';
    
    @Entry
    @Component
    struct Index {
      @Provide('NavPathStack') stack: NavPathStack = new NavPathStack();
    
      @Builder
      pageMap(name: string) {
        if (name === 'WebPage') {
          WebPage();
        }
      }
    
      build() {
        Column() {
          Navigation(this.stack) {
            Button('Navigation到本地H5页面')
              .onClick(() => {
                this.stack.pushPathByName('WebPage', null, false);
              });
          }
          .titleMode(NavigationTitleMode.Full)
          .mode(NavigationMode.Stack)
          .hideToolBar(true)
          .navDestination(this.pageMap);
        }
        .height('100%')
        .width('100%');
      }
    }
  3. 本地H5页面代码:
    <!DOCTYPE html>
    <html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </head>
    
    <body>
    <div class="button" onclick="buttonClicked('back')">返回上一级页面</div>
    </body>
    <script>
        let jsPort;
        window.onload = function() {
          console.info("网页加载完毕")
          window.addEventListener('message', function(event) {
            if (event.data === '__init_port__') {
              console.info("__init_port__", JSON.stringify(event.ports[0]))
              if (event.ports[0] != null) {
                // 1.保存从ets侧发送过来的端口
                jsPort = event.ports[0];
                jsPort.onmessage = function(event) {
                  // 2.接收ets侧发送过来的消息.
                  let msg = 'Got message from ets:';
                }
              }
            }
          })
        }
    
        function buttonClicked(value) {
          let data = {
            "method": value
          };
          if (jsPort) {
            jsPort.postMessage(JSON.stringify(data));
          }
        }
    </script>
    </html>

Q:Webview设置UA的时候为什么accessBackward返回都是false?

A:背景:

一般情况下,页面发生新的跳转时,前进后退栈会递增,此时accessBackward会返回true,表示存在后退历史节点。然而,在某些情况下,新的跳转不会新增历史节点,而是替换当前节点,比如当加载相同的URL,或者在初始URL加载过程中进行刷新时。

场景一:错误reload导致accessBackward与预期不符

解决措施:

在初始URL加载过程中进行刷新,例如refresh或通过setCustomUserAgent间接引起的自动刷新,会导致Web组件中的前进后退栈保持初始节点。此时,后续的跳转会替换掉该初始节点,而不会新增历史节点,accessBackward将返回false。

如果在页面加载过程中调用setCustomUserAgent会导致页面自动刷新,正确的使用方法请参考setCustomUserAgent接口使用。

场景二:因历史操作干预机制导致的accessBackward与预期不符

解决措施:

某些页面通过重定向或操作浏览器历史记录,导致用户无法或很难通过浏览器的backward返回到之前的页面,这会影响用户体验。历史操作干预机制通过跳过那些没有用户激活就添加的历史记录条目或重定向页面,来缓解这种问题。需要注意的是,这种干预仅影响浏览器的backward和forward操作,而不会影响前端的history.back()和history.forward()。

示例如下:

  1. 用户在a.com网站上,点击跳转到b.com。
  2. b.com使用pushState添加历史记录条目,或在未获得用户激活的情况下将用户导航到另一个页面(c.com)。
  3. 如果用户按下返回按钮,将跳过b.com,直接返回到a.com。