国产精品久久久aaaa,日日干夜夜操天天插,亚洲乱熟女香蕉一区二区三区少妇,99精品国产高清一区二区三区,国产成人精品一区二区色戒,久久久国产精品成人免费,亚洲精品毛片久久久久,99久久婷婷国产综合精品电影,国产一区二区三区任你鲁

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

鴻蒙ArkTS聲明式組件:Web

jf_46214456 ? 來源:jf_46214456 ? 作者:jf_46214456 ? 2024-07-04 15:35 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

Web

提供具有網頁顯示能力的Web組件,[@ohos.web.webview]提供web控制能力。

說明:
開發前請熟悉鴻蒙開發指導文檔 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]

  • 該組件從API Version 8開始支持。后續版本如有新增內容,則采用上角標單獨標記該內容的起始版本。
  • 示例效果請以真機運行為準,當前IDE預覽器不支持。

需要權限

ArkUI:

訪問在線網頁時需添加網絡權限:ohos.permission.INTERNET。

Android:

訪問在線網頁時需添加網絡權限:android.permission.INTERNET

iOS:

無需設置,應用通過詢問用戶獲取網絡權限。

跨平臺相關設置

無法訪問Http的設置

Android

targetSdkVersion版本升級到28之后,默認拒絕應用程序使用明文流量的請求,如http協議不再支持訪問。 需要在AndroidManifest.xml中開啟明文網絡流量解決此問題

android:usesCleartextTraffic="true"

如需詳細網絡安全設置也可以通過配置android:networkSecurityConfig屬性來進行詳細設置。

iOS

App Transport Security (ATS) 不支持訪問http服務。通過修改info.plist可以做到。

在源代碼模式修改比較方便。 在Xcode中右擊項目中的info.plist,選擇Open As > Source Code,在plist標簽中加入NSAppTransportSecurity。

示例如下:

< plist version="1.0" >
< dict >
	< key >NSAppTransportSecurity< /key >
	< dict >
		< key >NSAllowsArbitraryLoads< /key >
		< true/ >
	< /dict >
< /dict >
< /plist >

子組件

接口

Web(options: { src: ResourceStr, controller: WebviewController})

說明:

不支持轉場動畫。 同一頁面的多個web組件,必須綁定不同的WebviewController。

參數:

參數名參數類型必填參數描述
src[ResourceStr]網頁資源地址。如果訪問本地資源文件,請使用$rawfile。
controller[WebviewController9+]控制器

示例:

加載在線網頁

// xxx.ets
import web_webview from '@ohos.web.webview'

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController()
  build() {
    Column() {
      Web({ src: 'https://www.example.com', controller: this.controller })
    }
  }
}

加載本地網頁

// xxx.ets
import web_webview from '@ohos.web.webview'

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController()
  build() {
    Column() {
      // 通過$rawfile加載本地資源文件。
      Web({ src: $rawfile("index.html"), controller: this.controller })
    }
  }
}

加載的index.html文件,位于resources目錄下rawfile子目錄中

< !-- index.html -- >
< !DOCTYPE html >
< html >
    < body >
        < p >Hello World< /p >
    < /body >
< /html >

屬性

javaScriptAccess

javaScriptAccess(javaScriptAccess: boolean)

設置是否允許執行JavaScript腳本,默認允許執行。

參數:

參數名參數類型必填默認值參數描述
javaScriptAccessbooleantrue是否允許執行JavaScript腳本。

示例:

// xxx.ets
import web_webview from '@ohos.web.webview'

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController()
  build() {
    Column() {
      Web({ src: 'https://www.example.com', controller: this.controller })
        .javaScriptAccess(true)
    }
  }
}

zoomAccess

zoomAccess(zoomAccess: boolean)

設置是否支持手勢進行縮放,默認允許執行縮放。

參數:

參數名參數類型必填默認值參數描述
zoomAccessbooleantrue設置是否支持手勢進行縮放。

示例:

// xxx.ets
import web_webview from '@ohos.web.webview'

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController()
  build() {
    Column() {
      Web({ src: 'https://www.example.com', controller: this.controller })
        .zoomAccess(true)
    }
  }
}

onPageBegin

onPageBegin(callback: (event?: { url: string }) => void)

網頁開始加載時觸發該回調,且只在主frame觸發,iframe或者frameset的內容加載時不會觸發此回調。
Android和iOS的觸發時機與OpenHarmony不完全相同,以各平臺行為為準。

參數:

參數名參數類型參數描述
urlstring頁面的URL地址。

示例:

// xxx.ets
import web_webview from '@ohos.web.webview'

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController()

  build() {
    Column() {
      Web({ src: 'https://www.example.com', controller: this.controller })
        .onPageBegin((event) = > {
          if (event) {
            console.log('url:' + event.url)
          }
        })
    }
  }
}

onPageEnd

onPageEnd(callback: (event?: { url: string }) => void)

網頁加載完成時觸發該回調,且只在主frame觸發。

參數:

參數名參數類型參數描述
urlstring頁面的URL地址。

示例:

// xxx.ets
import web_webview from '@ohos.web.webview'

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController()

  build() {
    Column() {
      Web({ src: 'https://www.example.com', controller: this.controller })
        .onPageEnd((event) = > {
          if (event) {
            console.log('url:' + event.url)
          }
        })
    }
  }
}

onErrorReceive

onErrorReceive(callback: (event?: { request: WebResourceRequest, error: WebResourceError }) => void)

網頁加載遇到錯誤時觸發該回調。出于性能考慮,建議此回調中盡量執行簡單邏輯。

參數:

參數名參數類型參數描述
request[WebResourceRequest]網頁請求的封裝信息。
error[WebResourceError]網頁加載資源錯誤的封裝信息 。

示例:

// xxx.ets
import web_webview from '@ohos.web.webview'

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController()

  build() {
    Column() {
      Web({ src: 'https://www.example.com', controller: this.controller })
        .onErrorReceive((event) = > {
          if (event) {
            console.log('getErrorInfo:' + event.error.getErrorInfo())
            console.log('getErrorCode:' + event.error.getErrorCode())
            console.log('url:' + event.request.getRequestUrl())
          }
        })
    }
  }
}

minFontSize9+

minFontSize(size: number)

設置網頁字體大小最小值。

參數:

參數名參數類型必填默認值參數描述
sizenumber8設置網頁字體大小最小值,單位px。輸入值的范圍為-2^31到2^31-1,實際渲染時超過72的值按照72進行渲染,低于1的值按照1進行渲染。

示例:

// xxx.ets
import web_webview from '@ohos.web.webview'

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController()
  @State fontSize: number = 30
  build() {
    Column() {
      Web({ src: 'https://www.example.com', controller: this.controller })
        .minFontSize(this.fontSize)
    }
  }
}

horizontalScrollBarAccess9+

horizontalScrollBarAccess(horizontalScrollBar: boolean)

設置是否顯示橫向滾動條,包括系統默認滾動條和用戶自定義滾動條。默認顯示。

參數:

參數名參數類型必填默認值參數描述
horizontalScrollBarbooleantrue設置是否顯示橫向滾動條。

示例:

// xxx.ets
import web_webview from '@ohos.web.webview'

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController()
    
  build() {
    Column() {
      Web({ src: $rawfile('index.html'), controller: this.controller })
      .horizontalScrollBarAccess(true).height(150).width(200)
    }
  }
}

加載的html文件。

< !--index.html-- >
< !DOCTYPE html >
< html >
  < head >
    < title >Demo< /title >
    < style >
      body {
        width:3000px;
        height:3000px;
        padding-right:170px;
        padding-left:170px;
        border:5px solid blueviolet
      }
    < /style >
  < /head >
  < body >
    Scroll Test
  < /body >
< /html >

verticalScrollBarAccess9+

verticalScrollBarAccess(verticalScrollBar: boolean)

設置是否顯示縱向滾動條,包括系統默認滾動條和用戶自定義滾動條。默認顯示。

參數:

參數名參數類型必填默認值參數描述
verticalScrollBarAccessbooleantrue設置是否顯示縱向滾動條。

示例:

// xxx.ets
import web_webview from '@ohos.web.webview'

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new w eb_webview.WebviewController()

  build() {
    Column() {
      Web({ src: $rawfile('index.html'), controller: this.controller })
      .verticalScrollBarAccess(true).height(150).width(200)
    }
  }
}

加載的html文件。

< !--index.html-- >
< !DOCTYPE html >
< html >
  < head >
    < title >Demo< /title >
    < style >
      body {
        width:3000px;
        height:3000px;
        padding-right:170px;
        padding-left:170px;
        border:5px solid blueviolet
      }
    < /style >
  < /head >
  < body >
    Scroll Test
  < /body >
< /html >

backgroundColor

backgroundColor(ResourceColor:Color | number | string)

設置web組件背景顏色

參數:

ResourceColor

類型說明
[Color]顏色枚舉值。
numberHEX格式顏色,支持rgb。示例:0xffffff。
stringrgb或者argb格式顏色。示例:'#ffffff', '#ff000000', 'rgb(255, 100, 255)', 'rgba(255, 100, 255, 0.5)'。

示例:

// xxx.ets
import web_webview from '@ohos.web.webview';

@Entry
@Component
struct WebComponent {
controller1: web_webview.WebviewController = new web_webview.WebviewController();
controller2: web_webview.WebviewController = new web_webview.WebviewController();
controller3: web_webview.WebviewController = new web_webview.WebviewController();
controller4: web_webview.WebviewController = new web_webview.WebviewController();

build() {
  Column() {
    Text('設置backgroundColor為Color.Blue').height(50)
    Web({ src: 'https://www.example.com', controller: this.controller1 })
      .backgroundColor(Color.Blue).height(200)
      
    Text('設置backgroundColor為0x00ffff').height(50)
    Web({ src: 'https://www.example.com', controller: this.controller2 })
      .backgroundColor(0x00ffff).height(200)
      
    Text('設置backgroundColor為"#00FF00"').height(50)
    Web({ src: 'https://www.example.com', controller: this.controller3 })
      .backgroundColor('#00FF00').height(200)
      
    Text('設置backgroundColor為"rgb(255, 100, 255)"').height(50)
    Web({ src: 'https://www.example.com', controller: this.controller4 })
      .backgroundColor('rgb(255, 100, 255)').height(200)
  }
}
}

mediaPlayGestureAccess

mediaPlayGestureAccess(access: boolean)

設置有聲視頻播放是否需要用戶手動點擊,靜音視頻播放不受該接口管控,默認需要。 Android:該屬性只對網頁內嵌視頻播放生效。 iOS:不支持。

參數:

參數名參數類型必填默認值參數描述
accessbooleantrue設置有聲視頻播放是否需要用戶手動點擊。

示例:

// xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State access: boolean = true
    build() {
      Column() {
        Web({ src: 'https://www.example.com', controller: this.controller })
          .mediaPlayGestureAccess(this.access)
      }
    }
  }

onHttpErrorReceive

onHttpErrorReceive(callback: (event?: { request: WebResourceRequest, response: WebResourceResponse }) => void)

網頁加載資源遇到的HTTP錯誤(響應碼>=400)時觸發該回調。

參數:

參數名參數類型參數描述
request[WebResourceRequest]網頁請求的封裝信息。
response[WebResourceResponse]資源響應的封裝信息。

示例:

// xxx.ets
import web_webview from '@ohos.web.webview'

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController()

  build() {
    Column() {
      Web({ src: 'https://www.example.com', controller: this.controller })
        .onHttpErrorReceive((event) = > {
          if (event) {
            console.log('url:' + event.request.getRequestUrl())
            console.log('getResponseEncoding:' + event.response.getResponseEncoding())
            console.log('getResponseMimeType:' + event.response.getResponseMimeType())
            console.log('getResponseCode:' + event.response.getResponseCode())
            let result = event.request.getRequestHeader()
            console.log('The request header result size is ' + result.length)
            for (let i of result) {
              console.log('The request header key is : ' + i.headerKey + ' , value is : ' + i.headerValue)
            }
            let resph = event.response.getResponseHeader()
            console.log('The response header result size is ' + resph.length)
            for (let i of resph) {
              console.log('The response header key is : ' + i.headerKey + ' , value is : ' + i.headerValue)
            }
          }
        })
    }
  }
}

onProgressChange

onProgressChange(callback: (event?: { newProgress: number }) => void)

網頁加載進度變化時觸發該回調。

參數:

參數名參數類型參數描述
newProgressnumber新的加載進度,取值范圍為0到100的整數。

示例:

// xxx.ets
import web_webview from '@ohos.web.webview'

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController()

  build() {
    Column() {
      Web({ src: 'https://www.example.com', controller: this.controller })
        .onProgressChange((event) = > {
          if (event) {
            console.log('newProgress:' + event.newProgress)
          }
        })
    }
  }
}

onScroll9+

onScroll(callback: (event: {xOffset: number, yOffset: number}) => void)

通知網頁滾動條滾動位置。

參數:

參數名參數類型參數描述
xOffsetnumber以網頁最左端為基準,水平滾動條滾動所在位置。
yOffsetnumber以網頁最上端為基準,豎直滾動條滾動所在位置。

示例:

// xxx.ets
import web_webview from '@ohos.web.webview'

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController()
  build() {
    Column() {
      Web({ src: 'https://www.example.com', controller: this.controller })
      .onScroll((event) = > {
          console.info("x = " + event.xOffset)
          console.info("y = " + event.yOffset)
      })
    }
  }
}

onTitleReceive

onTitleReceive(callback: (event?: { title: string }) => void)

網頁document標題更改時觸發該回調。 Android和iOS的返回值與OpenHarmony不完全相同,以各平臺行為為準。

參數:

參數名參數類型參數描述
titlestringdocument標題內容。

示例:

// xxx.ets
import web_webview from '@ohos.web.webview'

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController()

  build() {
    Column() {
      Web({ src: 'https://www.example.com', controller: this.controller })
        .onTitleReceive((event) = > {
          if (event) {
            console.log('title:' + event.title)
          }
        })
    }
  }
}

onConsole

onConsole(callback: (event?: { message: ConsoleMessage }) => boolean)

通知宿主應用JavaScript console消息。

參數:

參數名參數類型參數描述
message[ConsoleMessage]觸發的控制臺信息。

返回值:

類型說明
boolean當返回true時,該條消息將不會再打印至控制臺,反之仍會打印至控制臺。iOS不支持。

示例:

// xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()

    build() {
      Column() {
        Web({ src: 'https://www.example.com', controller: this.controller })
          .onConsole((event) = > {
            if (event) {
              console.log('getMessage:' + event.message.getMessage())
              console.log('getMessageLevel:' + event.message.getMessageLevel())
            }
            return false
          })
      }
    }
  }

onScaleChange9+

onScaleChange(callback: (event: {oldScale: number, newScale: number}) => void)

當前頁面顯示比例的變化時觸發該回調。 Android和iOS的頁面顯示比例與OpenHarmony不完全相同,以各平臺行為為準。

參數:

參數名參數類型參數描述
oldScalenumber變化前的顯示比例百分比。
newScalenumber變化后的顯示比例百分比。

示例:

// xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()

    build() {
      Column() {
        Web({ src: 'https://www.example.com', controller: this.controller })
          .onScaleChange((event) = > {
            console.log('onScaleChange changed from ' + event.oldScale + ' to ' + event.newScale)
          })
      }
    }
  }

onLoadIntercept10+

onLoadIntercept(callback: (event?: { data: WebResourceRequest }) => boolean)

當Web組件加載url之前觸發該回調,用于判斷是否阻止此次訪問。默認允許加載。 在Android平臺,此接口在重定向時觸發。

參數:

參數名參數類型參數描述
request[WebResourceRequest]url請求的相關信息。

返回值:

類型說明
boolean返回true表示阻止此次加載,否則允許此次加載。

示例:

// xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()

    build() {
      Column() {
        Web({ src: 'https://www.example.com', controller: this.controller })
          .onLoadIntercept((event) = > {
            console.log('url:' + event.data.getRequestUrl())
            return true
          })
      }
    }
  }

onControllerAttached10+

onControllerAttached(callback: () => void)

當Controller成功綁定到Web組件時觸發該回調,并且該Controller必須為WebviewController,因該回調調用時網頁還未加載,無法在回調中使用有關操作網頁的接口,可以使用[loadUrl]等操作網頁不相關的接口。

示例:

在該回調中使用loadUrl加載網頁

// xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()

    build() {
      Column() {
        Web({ src: '', controller: this.controller })
          .onControllerAttached(() = > {
            this.controller.loadUrl($rawfile("index.html"));
          })
      }
    }
  }

加載的html文件。

< !-- index.html -- >
  < !DOCTYPE html >
  < html >
      < body >
          < p >Hello World< /p >
      < /body >
  < /html >

onAlert

onAlert(callback: (event?: { url: string; message: string; result: JsResult }) => boolean)

網頁觸發alert()告警彈窗時觸發回調。

參數:

參數名參數類型參數描述
urlstring當前顯示彈窗所在網頁的URL。
messagestring彈窗中顯示的信息。
result[JsResult]通知Web組件用戶操作行為,iOS端時result.handleCancel行為和result.handleConfirm一致。

返回值:

類型說明
boolean當回調返回true時,應用可以調用系統彈窗能力(包括確認和取消),并且需要根據用戶的確認或取消操作調用JsResult通知Web組件最終是否離開當前頁面。當回調返回false時,web組件暫不支持觸發默認彈窗。

示例:

// xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src: $rawfile("index.html"), controller: this.controller })
          .onAlert((event) = > {
            if (event) {
              console.log("event.url:" + event.url)
              console.log("event.message:" + event.message)
              AlertDialog.show({
                title: 'onAlert',
                message: 'text',
                primaryButton: {
                  value: 'cancel',
                  action: () = > {
                    event.result.handleCancel()
                  }
                },
                secondaryButton: {
                  value: 'ok',
                  action: () = > {
                    event.result.handleConfirm()
                  }
                },
                cancel: () = > {
                  event.result.handleCancel()
                }
              })
            }
            return true
          })
      }
    }
  }

onConfirm

onConfirm(callback: (event?: { url: string; message: string; result: JsResult }) => boolean)

網頁調用confirm()告警時觸發此回調。

參數:

參數名參數類型參數描述
urlstring當前顯示彈窗所在網頁的URL。
messagestring彈窗中顯示的信息。
result[JsResult]通知Web組件用戶操作行為。

返回值:

類型說明
boolean當回調返回true時,應用可以調用系統彈窗能力(包括確認和取消),并且需要根據用戶的確認或取消操作調用JsResult通知Web組件。當回調返回false時,web組件暫不支持觸發默認彈窗,跨平臺目前這個返回值沒有作用。

示例:

// xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()

    build() {
      Column() {
        Web({ src: $rawfile("index.html"), controller: this.controller })
          .onConfirm((event) = > {
            if (event) {
              console.log("event.url:" + event.url)
              console.log("event.message:" + event.message)
              AlertDialog.show({
                title: 'onConfirm',
                message: 'text',
                primaryButton: {
                  value: 'cancel',
                  action: () = > {
                    event.result.handleCancel()
                  }
                },
                secondaryButton: {
                  value: 'ok',
                  action: () = > {
                    event.result.handleConfirm()
                  }
                },
                cancel: () = > {
                  event.result.handleCancel()
                }
              })
            }
            return true
          })
      }
    }
  }

onPrompt9+

onPrompt(callback: (event?: { url: string; message: string; value: string; result: JsResult }) => boolean)

參數:

參數名參數類型參數描述
urlstring當前顯示彈窗所在網頁的URL。
messagestring彈窗中顯示的信息。
result[JsResult]通知Web組件用戶操作行為。

返回值:

類型說明
boolean當回調返回true時,應用可以調用系統彈窗能力(包括確認和取消),并且需要根據用戶的確認或取消操作調用JsResult通知Web組件。當回調返回false時,web組件暫不支持觸發默認彈窗,跨平臺目前這個返回值沒有作用。

示例:

// xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()

    build() {
      Column() {
        Web({ src: $rawfile("index.html"), controller: this.controller })
          .onPrompt((event) = > {
            if (event) {
              console.log("url:" + event.url)
              console.log("message:" + event.message)
              console.log("value:" + event.value)
              AlertDialog.show({
                title: 'onPrompt',
                message: 'text',
                primaryButton: {
                  value: 'cancel',
                  action: () = > {
                    event.result.handleCancel()
                  }
                },
                secondaryButton: {
                  value: 'ok',
                  action: () = > {
                    event.result.handlePromptConfirm(event.value)
                  }
                },
                cancel: () = > {
                  event.result.handleCancel()
                }
              })
            }
            return true
          })
      }
    }
  }

onHttpAuthRequest9+

onHttpAuthRequest(callback: (event?: { handler: HttpAuthHandler, host: string, realm: string}) => boolean)

通知收到http auth認證請求。

Android加載頁面不會直接觸發該回調,iOS加載頁面會直接觸發該回調。

參數:

參數名參數類型參數描述
handler[HttpAuthHandler]通知Web組件用戶操作行為。
hoststringHTTP身份驗證憑據應用的主機。
realmstringHTTP身份驗證憑據應用的域。

返回值:

類型說明
boolean返回false表示此次認證失敗,否則成功,跨平臺目前這個返回值沒有作用。

示例:

// xxx.ets
import web_webview from '@ohos.web.webview'

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController()
  httpAuth: boolean = false

  build() {
    Column() {
      Web({ src: 'https://www.example.com', controller: this.controller })
        .onHttpAuthRequest((event) = > {
          if (event) {
            AlertDialog.show({
              title: 'onHttpAuthRequest',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () = > {
                  event.handler.cancel()
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () = > {
                  event.handler.confirm("2222", "2222");
                }
              },
              cancel: () = > {
                event.handler.cancel()
              }
            })
          }
          return true
        })
    }
  }
}

onGeolocationShow

onGeolocationShow(callback: (event?: { origin: string, geolocation: JsGeolocation }) => void)

通知用戶收到地理位置信息獲取請求。

目前iOS不支持。

參數:

參數名參數類型參數描述
originstring指定源的字符串索引
geolocation[JsGeolocation]通知Web組件用戶操作行為。

示例:

// xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'https://www.example.com', controller:this.controller })
        .geolocationAccess(true)
        .onGeolocationShow((event) = > {
          if (event) {
            AlertDialog.show({
              title: 'title',
              message: 'text',
              confirm: {
                value: 'onConfirm',
                action: () = > {
                  event.geolocation.invoke(event.origin, true, true)
                }
              },
              cancel: () = > {
                event.geolocation.invoke(event.origin, false, true)
              }
            })
          }
        })
      }
    }
  }

onGeolocationHide

onGeolocationHide(callback: () => void)

通知用戶先前被調用[onGeolocationShow]時收到地理位置信息獲取請求已被取消。

目前iOS不支持。

參數:

參數名參數類型參數描述
callback() => void地理位置信息獲取請求已被取消的回調函數。

示例:

// xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'https://www.example.com', controller:this.controller })
        .geolocationAccess(true)
        .onGeolocationHide(() = > {
          console.log("onGeolocationHide...")
        })
      }
    }
  }

onPermissionRequest9+

onPermissionRequest(callback: (event?: { request: PermissionRequest }) => void)

通知收到獲取權限請求。

iOS監聽到webview權限申請的前提是要在plist設置app獲取權限選項,并且在首次打開應用,系統彈出獲取權限窗口時選擇授予。

Android監聽到webview權限申請的前提是要在Manifest中靜態配置。

getOrigin返回值以各平臺行為為準。

參數:

參數名參數類型參數描述
request[PermissionRequest]通知Web組件用戶操作行為。

示例:

// xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src: 'https://www.example.com', controller: this.controller })
          .onPermissionRequest((event) = > {
            if (event) {
              AlertDialog.show({
                title: 'title',
                message: 'text',
                primaryButton: {
                  value: 'deny',
                  action: () = > {
                    event.request.deny()
                  }
                },
                secondaryButton: {
                  value: 'onConfirm',
                  action: () = > {
                    event.request.grant(event.request.getAccessibleResource())
                  }
                },
                cancel: () = > {
                  event.request.deny()
                }
              })
            }
          })
      }
    }
  }

onPageVisible9+

onPageVisible(callback: (event: {url: string}) => void)

設置新頁面內容即將可見時觸發的回調函數。

獲取的url以各平臺行為為準。

參數:

參數名參數類型參數描述
urlstring新頁面內容即將可見時新頁面的url地址。

示例:

// xxx.ets
  import web_webview from '@ohos.web.webview'
  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    build() {
      Column() {
        Web({ src:'https://www.example.com', controller: this.controller })
         .onPageVisible((event) = > {
          console.log('onPageVisible url:' + event.url)
        })
      }
    }
  }

onDownloadStart

onDownloadStart(callback: (event?: { url: string, userAgent: string, contentDisposition: string, mimetype: string, contentLength: number }) => void)

通知主應用開始下載一個文件

返回信息以各平臺行為為準,跨平臺目前只支持獲取url, userAgent, mimetype, contentLength。

參數:

參數名參數類型參數描述
urlstring文件下載的URL。
userAgentstring用于下載的用戶代理。
mimetypestring服務器返回內容媒體類型(MIME)信息。
contentLengthcontentLength服務器返回文件的長度。

示例:

// xxx.ets
  import web_webview from '@ohos.web.webview'

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()

    build() {
      Column() {
        Web({ src: 'https://www.example.com', controller: this.controller })
          .onDownloadStart((event) = > {
            if (event) {
              console.log('url:' + event.url)
              console.log('userAgent:' + event.userAgent)
              console.log('mimetype:' + event.mimetype)
              console.log('contentLength:' + event.contentLength)
            }
          })
      }
    }
  }

onShowFileSelector9+

onShowFileSelector(callback: (event?: { result: FileSelectorResult, fileSelector: FileSelectorParam }) => boolean)

調用此函數以處理具有“文件”輸入類型的HTML表單,以響應用戶按下的“選擇文件”按鈕。

目前iOS不支持。

參數:

參數名參數類型參數描述
result[FileSelectorResult]用于通知Web組件文件選擇的結果。
fileSelector[FileSelectorParam]文件選擇器的相關信息。

返回值:

類型說明
boolean當返回值為true時,用戶可以調用系統提供的彈窗能力。當回調返回false時,web組件暫不支持觸發默認彈窗。

示例:

// xxx.ets
  import web_webview from '@ohos.web.webview';
  import picker from '@ohos.file.picker';
  import { BusinessError } from '@ohos.base';

  @Entry
  @Component
  struct WebComponent {
    controller: web_webview.WebviewController = new web_webview.WebviewController()
    @State uri: string = "file:///data/user/0/com.example.helloworld";

    build() {
      Column() {
        Web({ src: $rawfile('index.html'), controller: this.controller })
          .onShowFileSelector((event) = > {
            console.log('MyFileUploader onShowFileSelector invoked')
            event.result.handleFileList([this.uri]);
            return true
          })
      }
    }
  }

WebResourceError

web組件資源管理錯誤信息對象。

getErrorCode

getErrorCode(): number

獲取加載資源的錯誤碼。

錯誤碼:

Android和iOS的錯誤碼與OpenHarmony不完全相同,以各平臺錯誤碼為準。

返回值:

類型說明
number返回加載資源的錯誤碼。

getErrorInfo

getErrorInfo(): string

獲取加載資源的錯誤信息。
Android和iOS的錯誤信息與OpenHarmony不完全相同,以各平臺錯誤信息為準。

返回值:

類型說明
string返回加載資源的錯誤信息。

WebResourceRequest

web組件獲取資源請求對象。

getRequestUrl

getRequestUrl(): string

獲取資源請求的URL信息。

返回值:

類型說明
string返回資源請求的URL信息。

WebResourceResponse

web組件資源響應對象。

getResponseMimeType

getResponseMimeType(): string

獲取資源響應的媒體(MIME)類型。

返回值:

類型說明
string返回資源響應的媒體(MIME)類型。

getResponseEncoding

getResponseEncoding(): string

獲取資源響應的編碼。

返回值:

類型說明
string返回資源響應的編碼。

getResponseCode

getResponseCode(): number

獲取資源響應的狀態碼。

返回值:

類型說明
number返回資源響應的狀態碼。

ConsoleMessage

Web組件獲取控制臺信息對象。

getMessage

getMessage(): string

獲取ConsoleMessage的日志信息。

返回值:

類型說明
string返回ConsoleMessage的日志信息。

getMessageLevel

getMessageLevel(): MessageLevel

獲取ConsoleMessage的信息級別。

返回值:

類型說明
[MessageLevel]返回ConsoleMessage的信息級別。

MessageLevel枚舉說明

名稱描述
Debug調試級別。
Error錯誤級別。
Info消息級別。
Log日志級別。
Warn警告級別。

JsResult

Web組件返回的彈窗確認或彈窗取消功能對象。

handleCancel

handleCancel(): void

通知Web組件用戶取消彈窗操作。

handleConfirm

handleConfirm(): void

通知Web組件用戶確認彈窗操作。

handlePromptConfirm9+

handlePromptConfirm(result: string): void

通知Web組件用戶確認彈窗操作及對話框內容。

參數:

參數名參數類型必填默認值參數描述
resultstring-用戶輸入的對話框內容。

HttpAuthHandler9+

Web組件返回的http auth認證請求確認或取消和使用緩存密碼認證功能對象。

cancel9+

cancel(): void

通知Web組件用戶取消HTTP認證操作。

confirm9+

confirm(userName: string, pwd: string): boolean

使用用戶名和密碼進行HTTP認證操作。

參數:

參數名參數類型必填默認值參數描述
userNamestring-HTTP認證用戶名。
pwdstring-HTTP認證密碼。

返回值:

類型說明
boolean認證成功返回true,失敗返回false。跨平臺Android和iOS底層不會有返回值,所以都返回true。

isHttpAuthInfoSaved9+

isHttpAuthInfoSaved(): boolean

通知Web組件用戶使用服務器緩存的帳號密碼認證。

返回值:

類型說明
boolean存在密碼認證成功返回true,其他返回false。iOS底層不會有返回值,所以暫時在獲取不到服務器緩存帳號密碼的時候返回false,如果能獲取到就進行認證并返回true。

JsGeolocation

Web組件返回授權或拒絕權限功能的對象。

invoke

invoke(origin: string, allow: boolean, retain: boolean): void

設置網頁地理位置權限狀態。

參數:

參數名參數類型必填默認值參數描述
originstring-指定源的字符串索引。
allowboolean-設置的地理位置權限狀態。
retainboolean-是否允許將地理位置權限狀態保存到系統中。

PermissionRequest9+

Web組件返回授權或拒絕權限功能的對象。

deny9+

deny(): void

拒絕網頁所請求的權限。

getOrigin9+

getOrigin(): string

獲取網頁來源。

返回值:

類型說明
string當前請求權限網頁的來源。

getAccessibleResource9+

getAccessibleResource(): Array

獲取網頁所請求的權限資源列表,跨平臺資源列表支持的類型有RESOURCE_VIDEO_CAPTURE和RESOURCE_AUDIO_CAPTURE。

返回值:

類型說明
Array網頁所請求的權限資源列表。

grant9+

grant(resources: Array): void

對網頁訪問的給定權限進行授權,跨平臺iOS不支持授予某一種類型的權限,只支持授予當前申請的權限,或拒絕當前申請的權限。

參數:

參數名參數類型必填默認值參數描述
resourcesArray-授予網頁請求的權限的資源列表,跨平臺iOS此參數沒有作用。

FileSelectorResult9+

通知Web組件的文件選擇結果。

handleFileList9+

handleFileList(fileList: Array): void

通知Web組件進行文件選擇操作。

參數:

參數名參數類型必填默認值參數描述
fileListArray-需要進行操作的文件列表。

FileSelectorParam9+

web組件獲取文件對象。

getTitle9+

getTitle(): string

獲取文件選擇器標題。

返回值:

類型說明
string返回文件選擇器標題。

getMode9+

getMode(): FileSelectorMode

獲取文件選擇器的模式。

返回值:

類型說明
[FileSelectorMode]返回文件選擇器的模式。

getAcceptType9+

getAcceptType(): Array

獲取文件過濾類型。

返回值:

類型說明
Array返回文件過濾類型。

isCapture9+

isCapture(): boolean

獲取是否調用多媒體能力。

返回值:

搜狗高速瀏覽器截圖20240326151450.png

類型說明HarmonyOS與OpenHarmony鴻蒙文檔籽料:mau123789是v直接拿
boolean返回是否調用多媒體能力。

FileSelectorMode枚舉說明

名稱描述
FileOpenMode打開上傳單個文件。
FileOpenMultipleMode打開上傳多個文件。
FileOpenFolderMode打開上傳文件夾模式。
FileSaveMode文件保存模式。

審核編輯 黃宇

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • 組件
    +關注

    關注

    1

    文章

    573

    瀏覽量

    19022
  • 鴻蒙
    +關注

    關注

    60

    文章

    2963

    瀏覽量

    45891
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關推薦
    熱點推薦

    基于凌羽派的OpenHarmony北向應用開發:ArkTS語法-數據類型和變量聲明

    包含初始值,開發者無需顯指定類型,因為ArkTS規范已列舉了所有允許自動推斷類型的場景。 以下示例中,兩條聲明語句都是有效的,兩個變量都是string類型: let hi1: string
    發表于 02-26 14:24

    鴻蒙非侵入彈窗新解法,企查查正式開源“QuickDialog”彈窗組件

    近日,企查查將其自研的鴻蒙彈窗組件庫“QuickDialog”開源,并上線至?OpenHarmony 三方庫中心倉。這是鴻蒙生態首個支持“彈窗堆棧暫存能力”的非侵入彈窗解決方案,憑借
    的頭像 發表于 07-31 10:40 ?729次閱讀
    <b class='flag-5'>鴻蒙</b>非侵入<b class='flag-5'>式</b>彈窗新解法,企查查正式開源“QuickDialog”彈窗<b class='flag-5'>組件</b>庫

    【HarmonyOS 5】金融應用開發鴻蒙組件實踐

    【HarmonyOS 5】金融應用開發鴻蒙組件實踐 ##鴻蒙開發能力 ##HarmonyOS SDK應用服務##鴻蒙金融類應用 (金融理財# 一、
    的頭像 發表于 07-11 18:20 ?960次閱讀
    【HarmonyOS 5】金融應用開發<b class='flag-5'>鴻蒙</b><b class='flag-5'>組件</b>實踐

    【 HarmonyOS 5 入門系列 】鴻蒙HarmonyOS示例項目講解

    【 HarmonyOS 5 入門系列 】鴻蒙HarmonyOS示例項目講解 ##鴻蒙開發能力 ##HarmonyOS SDK應用服務##鴻蒙金融類應用 (金融理財# 一、前言:移動開發聲明
    的頭像 發表于 07-07 11:57 ?1082次閱讀
    【 HarmonyOS 5 入門系列 】<b class='flag-5'>鴻蒙</b>HarmonyOS示例項目講解

    ArkUI介紹

    范式,分別是基于ArkTS聲明開發范式(簡稱“聲明開發范式”)和兼容JS的類Web開發范式
    發表于 06-24 06:41

    UI開發概述

    應用開發在TypeScript(簡稱TS)生態基礎上做了進一步擴展。擴展能力包含聲明UI描述、自定義組件、動態擴展UI元素、狀態管理和渲染控制。狀態管理作為基于ArkTS
    發表于 06-24 06:36

    什么是ArkTS

    ArkTS簡介 ArkTS是OpenHarmony優選的應用高級開發語言。ArkTS提供了聲明UI范式、狀態管理支持等相應的能力,讓開發
    發表于 06-17 06:24

    鴻蒙5開發寶藏案例分享---Web開發優化案例分享

    ;gt;ArkWeb</span>(方舟Web組件加載Web頁面的優化技巧,簡直是提升應用流暢度的神兵利器。官方文檔寫得比較“正經”,我這就把它掰開了、揉碎了,加上我自己
    發表于 06-12 17:20

    鴻蒙5開發寶藏案例分享---Web加載時延優化解析

    鴻蒙開發寶藏:Web加載完成時延優化實戰 大家好呀!今天在翻鴻蒙開發者文檔時,發現了一個隱藏的 性能優化寶藏區 ——官方竟然悄悄提供了超多實戰案例!尤其是****Web加載完成時延分析
    發表于 06-12 17:11

    鴻蒙5開發寶藏案例分享---Web頁面內點擊響應時延分析

    */ background: white; /* 覆蓋透明背景 */ } Web組件初始化加速 // ArkTS側提前初始化Web組件
    發表于 06-12 17:09

    使用DevEcoStudio 開發、編譯鴻蒙 NEXT_APP 以及使用中文插件

    使用 ArkTS 聲明語法: @Entry @Component export struct Index { @State message: string = \'Hello World
    發表于 06-11 17:18

    Kuikly鴻蒙版正式開源 —— 揭秘卓越性能適配之旅

    的 ArkUI 來編寫的,UI組件由數據和UI描述組成,UI更新只能通過修改其綁定的數據來實現。渲染層怎樣驅動聲明的ArkUI成為了鴻蒙版適配的第一個問題。 初步解決方案:統一Bui
    發表于 06-04 16:46

    KaihongOS操作系統:ArkTS語言基礎

    和特性都適用于ArkTSArkTS為TypeScript添加了一些特定的API和組件,以便更好地在KaihongOS上進行開發。 ArkTS基礎 類和接口 在
    發表于 04-23 06:31

    開源啦!!!基于鴻蒙ArkTS封裝的圖表組件《McCharts》,大家快來一起共創

    Hello;大家好,我是陳楊。好久沒更新了,首先是自己本職工作比較忙,基本沒時間寫作。其次就是學習技術,自學鴻蒙ArkTS語言已經接近半年了,也算半路出師了,這次將分享我封裝的組件庫,所以有啥講錯
    發表于 03-15 15:21

    「極速探索HarmonyOS NEXT 」閱讀體驗】+Web組件

    ,則源于web開發。盡管Web應用在性能上略遜一籌,但由于其龐大的用戶使用基數,在諸多場景下仍不可或缺。 在應用中顯示 Web 頁面 在開發中使用 Web
    發表于 03-10 10:39