目录

如何解决Web组件加载H5页面时拉起系统拨号等功能出现白屏

问题现象

Web加载H5页面,点击H5中“tel:”、“sms:”、“mailto:”开头的特殊链接,没有跳转到对应的拨号、短信、邮件应用,而是出现Web组件白屏现象。代码如下:

import { webview } from '@kit.ArkWeb';


@Entry
@Component
struct WebComponent {
  webviewController: webview.WebviewController = new webview.WebviewController();


  build() {
    Column() {
      Web({ src: $rawfile('call.html'), controller: this.webviewController })
    }
  }
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
</head>
<body>
<div>
    <a href="tel:xxx">拨打电话</a>
    <a href="sms:xxx">发送信息</a>
    <a href="mailto:xxx@example">发送邮件</a>
</div>
</body>
</html>

背景知识

问题定位

Web组件不支持通过“tel:”、“sms:”、“mailto:”链接直接拉起对应应用(拨号、短信、邮件)。

分析结论

Web组件不支持通过“tel:”、“sms:”、“mailto:”链接直接拉起对应应用(拨号、短信、邮件),需要通过onLoadIntercept回调拦截这些链接,调用ArkTS API拉起对应应用。

修改建议

通过Web组件的onLoadIntercept回调拦截“tel:”、“sms:”、“mailto:”链接跳转,调用ArkTS API拉起对应应用。示例代码如下:

import { call } from '@kit.TelephonyKit';
import { common, Want } from '@kit.AbilityKit';
import { webview } from '@kit.ArkWeb';


const SUB_COUNT: number = 4;


@Entry
@Component
struct Index {
  webviewController: webview.WebviewController = new webview.WebviewController();


  build() {
    Column() {
      Web({ src: $rawfile('call.html'), controller: this.webviewController })
        .fileAccess(false)
        .geolocationAccess(false)
        .onLoadIntercept((event) => {
          if (event) {
            let url: string = event.data.getRequestUrl();
            // 判断链接是否为拨号链接
            if (url.indexOf('tel:') === 0) {
              // 跳转拨号界面
              call.makeCall(url.substring(SUB_COUNT), (err) => {
                if (!err) {
                  console.info('make call succeeded.');
                } else {
                  console.error(`make call fail, err is: ${err}`);
                }
              });
              return true;
            }
            // 判断链接是否为发短信链接
            if (url.indexOf('sms:') === 0) {
              // 跳转到短信编辑界面
              let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
              let want: Want = {
                bundleName: 'com.ohos.mms',
                action: 'ohos.want.action.viewData',
                uri: url,
              };
              context.startAbility(want).then((data) => {
                console.info(`Success, ${data}`);
              }).catch(() => {
                console.error('error');
              });
              return true;
            }
            // 判断链接是否为发邮件链接
            if (url.indexOf('mailto:') === 0) {
              // 跳转到邮件应用
              let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
              let want: Want = {
                action: 'ohos.want.action.sendToData',
                uri: url
              };
              context.startAbility(want).then((data) => {
                console.info(`Success, ${data}`);
              }).catch(() => {
                console.error('error');
              });
              return true;
            }
          }
          return false;
        });
    };
  }
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
</head>
<body>
<div>
    <a href="tel:xxx">拨打电话</a>
    <a href="sms:xxx">发送信息</a>
    <a href="mailto:xxx@example">发送邮件</a>
</div>
</body>
</html>