native.newWebView()

类型 函数
native.*
返回值 WebView
版本 发行版 2024.3703
关键词 网络视图、网站、网络叠加
另请参阅 native.showWebPopup()
urlRequest

概述

加载网络视图容器中的网页。此本机网络视图可以通过 x/y 属性移动,方式与其他显示对象相同。在 iOS 上,还可以通过 rotation 属性对其进行旋转。

此 API 支持从包含 HTML 内容的本地文件(位于系统目录之一中)或从远程网站加载。

需要注意的地方

一般

  • 本机网络视图不属于 OpenGL 画布的一部分,并且不遵守显示对象层次结构,因此它们始终显示在法线显示对象(包括图像、文本和矢量对象)的前面。

  • 此 API 不应与类似的(较旧)native.showWebPopup()混淆。

  • 必须通过调用 removeSelf() 方法来关闭网络视图,因为在监听函数中返回 false 不起作用(对于与网络视图不同的网络快显窗口,在监听器中返回 false 会关闭网络快显窗口,但对本机网络视图并非如此)。

  • tvOS 不支持本机网络视图。

  • 在 iOS 上,本机网络视图只能通过 rotation 属性进行旋转。

iOS

在 iOS 上,在网络视图监听器中检查 event.errorMessage 时,偶尔会收到诸如NSURLerrorDomain error -999之类的错误消息。这是在取消异步加载时返回的。WebKit 框架委托在对加载的资源执行取消操作时会收到此错误,但是如果下载被取消,NSURLConnectionNSURLDownload 委托将不会收到此错误。如果在网络视图仍加载第一页时单击了网络视图中的链接,则可能会发生此事件/错误。一般来说,可以忽略此错误。

Android

  • 如果网络快显窗口从互联网显示网页,则必须将 INTERNET 权限添加到 build.settings 文件。如果网络视图只显示不访问互联网的本地 HTML 文件,则该权限是不必要的。
settings =
{
    android =
    {
        usesPermissions =
        {
            "android.permission.INTERNET",
        },
    },
}

语法

native.newWebView( x, y, width, height )
x (必需)

数字. 与网络视图对象的中心对应的 x 坐标。

y (必需)

数字. 与网络视图对象的中心对应的 y 坐标。

width (必需)

Number。Web 视图对象的宽度。

height (必需)

Number。Web 视图对象的高度。

属性/方法

请参阅WebView文档,了解相关方法和属性的列表。

事件

请参阅urlRequest事件文档,了解与各种WebView对象事件相关的属性。

示例

基本网络视图
local webView = native.newWebView( display.contentCenterX, display.contentCenterY, 320, 480 )
webView:request( "https://solar2d.com/" )
-- or
webView:request( "localfile.html", system.ResourceDirectory )
相同的回调监听响应
local function webListener( event )
    if event.url then
        print( "You are visiting: " .. event.url )
    end
 
    if event.type then
        print( "The event.type is " .. event.type ) -- print the type of request
    end
 
    if event.errorCode then
        native.showAlert( "Error!", event.errorMessage, { "OK" } )
    end
end
 
local webView = native.newWebView( display.contentCenterX, display.contentCenterY, 320, 480 )
webView:request( "https://solar2d.com/" )
 
webView:addEventListener( "urlRequest", webListener )