HarmonyOS webview如何加载沙箱html

HarmonyOS  webview如何加载沙箱html 。

HarmonyOS
2024-09-05 12:47:59
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu
import web_webview from '@ohos.web.webview' 
import mediaquery from '@ohos.mediaquery'; 
import window from '@ohos.window'; 
import common from '@ohos.app.ability.common'; 
import { BusinessError } from '@ohos.base'; 
import inputMethod from '@ohos.inputMethod'; 
let context = getContext(this) as common.UIAbilityContext; 
let filesDir = context.filesDir 
let inputMethodController = inputMethod.getController(); 
@Entry 
@Component 
struct WebComponent { 
  controller: web_webview.WebviewController = new web_webview.WebviewController() 
  @State portraitFunc:mediaquery.MediaQueryResult|void|null = null; 
  controller1: TextInputController = new TextInputController() 
  @State text: string = '' 
 
  handler: FullScreenExitHandler | null = null 
  // 当设备横屏时条件成立 
  listener:mediaquery.MediaQueryListener = mediaquery.matchMediaSync('(orientation: landscape)'); 
  onPortrait(mediaQueryResult:mediaquery.MediaQueryResult) { 
  } 
  onPageShow(){ 
    //输入框显示的时候,通过focusControl.requestFocus 主动让焦点转移至参数指定的组件上。 
    focusControl.requestFocus('aaa'); 
  } 
  aboutToAppear() { 
    web_webview.WebviewController.setWebDebuggingAccess(true); 
    this.listener.on('change', (mediaQueryResult:mediaquery.MediaQueryResult) => { this.onPortrait(mediaQueryResult) }); 
    try { 
      let textConfig: inputMethod.TextConfig = { 
        inputAttribute: { 
          textInputType: 0, 
          enterKeyType: 1 
        } 
      }; 
      inputMethodController.attach(true, textConfig, (err: BusinessError) => { 
        if (err) { 
          console.error(`Failed to attach: ${JSON.stringify(err)}`); 
          return; 
        } 
        console.log('Succeeded in attaching the inputMethod.'); 
      }); 
    } catch(err) { 
      console.error(`Failed to attach: ${JSON.stringify(err)}`); 
    } 
 
  } 
  // 改变设备横竖屏状态函数 
  private changeOrientation(isLandscape: boolean) { 
    // 获取UIAbility实例的上下文信息 
    let context:common.UIAbilityContext = getContext(this) as common.UIAbilityContext; 
    // 调用该接口手动改变设备横竖屏状态 
    window.getLastWindow(context).then((lastWindow) => { 
      lastWindow.setPreferredOrientation(isLandscape ? window.Orientation.LANDSCAPE : window.Orientation.PORTRAIT) 
    }); 
  } 
  build() { 
    Column() { 
      Row().height(50) 
      Button('1') 
        .onClick(()=>{ 
          this.changeOrientation(true); 
        }) 
      Button('2').onClick(()=>{ 
        this.changeOrientation(false); 
      }) 
      // Button('刷新').onClick(()=>{ 
      //  // this.controller.refresh() 
      //   inputMethodController.showTextInput((err: BusinessError) => { 
      //     if (err) { 
      //       console.error(`Failed to showTextInput: ${JSON.stringify(err)}`); 
      //       return; 
      //     } 
      //     console.log('Succeeded in showing the inputMethod.'); 
      //   }); 
      // }) 
      //TextInput({ text: this.text, placeholder: 'input your word...', controller: this.controller1 }).defaultFocus(true) 
      Web({ 
        //src: '', 
        src: 'file://'+filesDir+'/example.html', 
        controller: this.controller}) 
        //.defaultFocus(true) 
        .onInterceptRequest((event)=>{ 
          if (event){ 
            console.log('url为: '+event.request.getRequestUrl()) 
          } 
          return null 
        }) 
        .key('aaa') 
          //.defaultFocus(true) 
        .onControllerAttached(()=>{ }) 
        .onPageVisible(()=>{ 
          inputMethodController.showTextInput().then(() => { 
            console.log('Succeeded in showing text input.'); 
          }).catch((err: BusinessError) => { 
            console.error(`Failed to showTextInput: ${JSON.stringify(err)}`); 
          }); 
        }) 
        .mixedMode(MixedMode.All) 
        .javaScriptAccess(true) 
        .domStorageAccess(true) 
 
      // .backgroundColor(Color.Blue) 
      //.height('100%') 
    } 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.

关键代码。

let context = getContext(this) as common.UIAbilityContext; 
let filesDir = context.filesDir 
Web({ 
 
  src: 'file://'+filesDir+'/example.html',
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
分享
微博
QQ
微信
回复
2024-09-05 16:36:18
相关问题
HarmonyOS webview组件如何加载html代码?
1072浏览 • 1回复 待解决
HarmonyOS webview加载本地html问题
1657浏览 • 1回复 待解决
HarmonyOS webview加载html string一直闪退
762浏览 • 1回复 待解决
HarmonyOS webview如何播放本地沙箱视频
2663浏览 • 1回复 待解决
webview 如何显示纯文本html内容?
2908浏览 • 1回复 待解决
HarmonyOS 如何加载本地沙盒中的html
971浏览 • 1回复 待解决
HarmonyOS Web组件如何加载html字符串
1593浏览 • 1回复 待解决
鸿蒙是否支持加载HTML
13389浏览 • 3回复 待解决
HarmonyOS web从本地沙箱加载
819浏览 • 1回复 待解决
HarmonyOS web加载沙箱文件失败
645浏览 • 1回复 待解决
HarmonyOS 如何监听Webview加载失败
611浏览 • 1回复 待解决
HarmonyOS Web组件加载html文件异常
1326浏览 • 1回复 待解决
如何使用Image加载沙箱路径图片资源
2288浏览 • 2回复 待解决