HarmonyOS this.controller.accessBackward() 返回值一直返回false

升级到api 12以后,判断当前网页是否可以返回方法一直返回false

HarmonyOS
2024-12-25 08:06:55
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

setCustomUserAgent设置后与web页面的跳转时序是web跳转后才设置UserAgent,这就导致页面跳转了但新agent关联的页面堆栈数仍只有一个,webviewController.accessBackward()总是返回false。

参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-webview-V5#setcustomuseragent10

建议在setCustomUserAgent方法后,调用loadUrl,且使用onControllerAttached替换onLoadIntercept进行applyCustomUserAgent方法的调用,参考demo如下:

import { webview } from '@kit.ArkWeb'

@Entry
@Component
export struct PageWeb {
  readonly webUrl: string | Resource = 'www.myURL.com'
  readonly controller: WebviewController = new webview.WebviewController()
  userAgentAlreadySet: boolean = false
  @State customUserAgent?: string = undefined
  build() {
    if (this.customUserAgent){
      Web({ src:'', controller: this.controller })
        .size({ width: '100%', height: '100%' })
        .javaScriptAccess(true)
        .fileAccess(true)
        .domStorageAccess(true)
        .databaseAccess(true)
        .mixedMode(MixedMode.Compatible)
        .zoomAccess(true)
        .geolocationAccess(true)
        .multiWindowAccess(false)
        .overviewModeAccess(false)
        .darkMode(WebDarkMode.Auto)
        .pinchSmooth(true)
        .onControllerAttached(() => {
          this.applyCustomUserAgent()
        })}
  }


  aboutToAppear(): void {
    // 模拟异步构建 customUserAgent 过程
    setTimeout(() => this.customUserAgent = 'ua/value', 200)
  }


  applyCustomUserAgent(): void {
    if (this.userAgentAlreadySet || !this.customUserAgent) return
    this.userAgentAlreadySet = true
    let defaultUserAgent = this.controller.getUserAgent()
    this.controller.setCustomUserAgent(defaultUserAgent + ' ' + this.customUserAgent)
    this.controller.loadUrl(this.webUrl)
  }


  onBackPress(): boolean | void {
    if (this.controller.accessBackward()) {
      this.controller.backward()
      return true
    }
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-25 10:16:08
相关问题
HarmonyOS accessBackward方法一直返回true
778浏览 • 1回复 待解决
HarmonyOS TextPickerDialog返回值问题
1042浏览 • 1回复 待解决
HarmonyOS onBackPress返回值问题
747浏览 • 1回复 待解决
HarmonyOS Promise函数返回值问题
1117浏览 • 1回复 待解决
event.data.isRequestGesture()返回值问题
1679浏览 • 1回复 待解决
求助:关于HTTP返回值的问题
3684浏览 • 1回复 待解决
HarmonyOS crypto-js加密没有返回值
866浏览 • 1回复 待解决
HarmonyOS router.back()如何获取返回值
1195浏览 • 1回复 待解决
HarmonyOS 使用电池状态返回值问题
1297浏览 • 1回复 待解决
错误rowCount返回值-1如何处理?
2158浏览 • 1回复 待解决
HarmonyOS获取图片旋转一直报错
1179浏览 • 1回复 待解决
HarmonyOS 获取副卡服务商无返回值
802浏览 • 1回复 待解决