HarmonyOS鸿蒙Next中为什么customizeSchemes会失败

HarmonyOS鸿蒙Next中为什么customizeSchemes会失败

在aboutToAppear中调用customizeSchemes时抛出Failed to register custom schemes:

aboutToAppear() {
  web_webview.WebviewController.customizeSchemes([{
      schemeName: 'hbfont',
      isSupportFetch: true,
      isSupportCORS: true,
  }])
}
Error message:Failed to register custom schemes.
Error code:17100020
Stacktrace:
    at aboutToAppear (ohos/src/main/ets/components/plugin/webview/in_app_webview/OhosWebView.ets:66:5)
    at buildWithNestingBuilder (../../../foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/jsXNode.js:203:1)
    at build (../../../foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/jsXNode.js:212:1)
    at build (../../../foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/jsXNode.js:70:1)
    at makeNode (oh_modules/.ohpm/@ohos+flutter_ohos@n0mf9e7azksqb+fwiimvqz+w1mlb1i+vphyco6m4h7s=/oh_modules/@ohos/flutter_ohos/src/main/ets/embedding/ohos/EmbeddingNodeController.ets:58:7)
    at __makeNode__ (../../../foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/jsXNode.js:710:1)
    at anonymous (oh_modules/.ohpm/@ohos+flutter_ohos@n0mf9e7azksqb+fwiimvqz+w1mlb1i+vphyco6m4h7s=/oh_modules/@ohos/flutter_ohos/src/main/ets/view/DynamicView/dynamicView.ets:253:5)
    at updateFunc (/usr1/hmos_for_system/src/increment/sourcecode/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6813:1)
    at observeComponentCreation2 (/usr1/hmos_for_system/src/increment/sourcecode/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6840:1)
    at buildNodeContainer (oh_modules/.ohpm/@ohos+flutter_ohos@n0mf9e7azksqb+fwiimvqz+w1mlb1i+vphyco6m4h7s=/oh_modules/@ohos/flutter_ohos/src/main/ets/view/DynamicView/dynamicView.ets:252:21)
    at anonymous (oh_modules/.ohpm/@ohos+flutter_ohos@n0mf9e7azksqb+fwiimvqz+w1mlb1i+vphyco6m4h7s=/oh_modules/@ohos/flutter_ohos/src/main/ets/view/DynamicView/dynamicView.ets:282:7)
    at ifElseBranchUpdateFunction (/usr1/hmos_for_system/src/increment/sourcecode/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:4300:1)
    at anonymous (oh_modules/.ohpm/@ohos+flutter_ohos@n0mf9e7azksqb+fwiimvqz+w1mlb1i+vphyco6m4h7s=/oh_modules/@ohos/flutter_ohos/src/main/ets/view/DynamicView/dynamicView.ets:281:56)
    at updateFunc (/usr1/hmos_for_system/src/increment/sourcecode/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6813:1)
    at observeComponentCreation2 (/usr1/hmos_for_system/src/increment/sourcecode/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6840:1)
    at initialRender (oh_modules/.ohpm/@ohos+flutter_ohos@n0mf9e7azksqb+fwiimvqz+w1mlb1i+vphyco6m4h7s=/oh_modules/@ohos/flutter_ohos/src/main/ets/view/DynamicView/dynamicView.ets:268:3)
    at initialRenderView (/usr1/hmos_for_system/src/increment/sourcecode/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6492:1)
    at (entry/src/main/ets/pages/Index.ets:30:7)

更多关于HarmonyOS鸿蒙Next中为什么customizeSchemes会失败的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

试了没有复现问题

import { webview as web_webview } from '@kit.ArkWeb'

@Component
struct Index {
  private webViewController: web_webview.WebviewController = new web_webview.WebviewController()
  responseWeb: WebResourceResponse = new WebResourceResponse();


  aboutToAppear() {
    web_webview.WebviewController.customizeSchemes([{
      schemeName: 'hbfont',
      isSupportFetch: true,
      isSupportCORS: true}
      ])
    }

  build() {
    Column() {
      Web({ src: '', controller: this.webViewController })
        .onInterceptRequest((event) => {
          if (event) {
            console.log('url:' + event.request.getRequestUrl());
          }
          return this.responseWeb;
        })
    }
    .height('100%')
    .width('100%')
  }
}

更多关于HarmonyOS鸿蒙Next中为什么customizeSchemes会失败的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中,customizeSchemes失败通常由以下原因导致:

  1. 配置文件中scheme名称重复或格式错误
  2. ability的exported属性未设置为true
  3. 设备类型过滤配置不当
  4. 目标ability未在manifest.json中正确声明

检查要点:

  • scheme名称必须唯一且符合格式规范(字母开头,仅含字母数字和.
  • 确保目标ability的skills配置包含对应的scheme
  • 验证deviceType是否匹配运行环境

在HarmonyOS Next中,customizeSchemes调用失败通常有几个常见原因:

  1. 注册时机问题:customizeSchemes必须在WebviewController实例创建之前调用。在aboutToAppear中调用可能已经太晚,建议在应用启动时或页面初始化阶段更早调用。

  2. 参数格式错误:您的代码示例中缺少右花括号,正确的格式应该是:

    web_webview.WebviewController.customizeSchemes([{
        schemeName: 'hbfont',
        isSupportFetch: true,
        isSupportCORS: true
    }])
    
  3. 重复注册:同一个schemeName只能注册一次,重复注册会导致失败。

  4. 权限问题:确保在config.json中声明了必要的网络权限:

    "reqPermissions": [
        {
            "name": "ohos.permission.INTERNET"
        }
    ]
    
  5. schemeName格式:schemeName必须符合URL scheme规范,建议使用小写字母且不包含特殊字符。

建议检查以上几点,特别是代码中的语法错误和调用时机问题。错误码17100020通常表示参数无效或注册时机不正确。

回到顶部