在应用程序中经常会用到浏览器去打开网页,就像之前文章里面提到的一样,我们还会用它来解决排版的问题。今天,我们来简单介绍一下如果使用Corona 中的“Web Popups”。
native.showWebPopup()
native.showWebPopup( x, y, width, height, url [, options] )
-- or just...
native.showWebPopup( url [, options] )
我们可以用它来打开网页,例如:native.showWebPopup( "http://www.buildapp.net/" )
它有以下几个参数:
- options.baseUrl
- options.hasBackground
- options.urlRequest
打开远程页面:native.showWebPopup( "http://www.buildapp.net/", { hasBackground=true, urlRequest=webListener } )
打开本地页面:native.showWebPopup( "appfiles/info.html", { baseUrl=system.ResourceDirectory } )
baseURL可以是以下参数:
- system.TemporaryDirectory
- system.DocumentsDirectory
您可能已经注意到,X,Y,宽度和高度参数,可以加到native.showWebPopup()。如果不设置,默认情况下,网页将占用您的设备全屏宽度和高度。如果你不想弹出网页占用整个屏幕,例如,如果你想点击按钮让网页出现在窗口的上方或下方,你可以在调用native.showWebPopup()时设置X,Y ,宽度和高度。
urlRequest监听器,可以监听:
网页弹出显示。
如果用户触摸的URLRequest侦听器函数(如果指定)一个环节,被调用。
如果该函数返回true(或者如果没有指定的侦听器函数),页面被载入。
如果函数返回假,网页弹出关闭。
local function listener( event )
local shouldLoad = true
local url = event.url
if 1 == string.find( url, "corona:close" ) then
-- Close the web popup
shouldLoad = false
end
if event.errorCode then
-- Error loading page
print( "Error: " .. tostring( event.errorMessage ) )
shouldLoad = false
end
return shouldLoad
end
上面这个例子清楚地表明你可以在检测到在一个特定的URL字符串之后,关闭网页,但你可以做的还远远不止这些。
native.cancelWebPopup()
如果你想关闭网页,可以调用:native.cancelWebPopup()。这个函数可以关闭任何已经打开的网页,你可以在任何时候调用,可以在你的应用程序中点击一个按钮来关闭网页。
不可见背景
这个很牛逼,就是说,您创建了一个webPopup之后,在页面加载完成之前,它的背景是透明的,不会让你的应用程序界面很难看。
本地HTML
注意一点的是,如果你打开本地HTML文件,需要在文件的<head></head>标签内加入这行:
<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
ipad里要把width设置成1024.