native.newWebView()

Type Function
Library native.*
Return value WebView
Revision Current Public Release (2014.2393)
Keywords web view, website, web overlay
See also native.showWebPopup()

Overview

Loads a remote web page in a web view container. Native web views differ from web popups in that you can move them (via x/y properties), rotate them (via the rotation property), or assign physics bodies to them in the same manner you would any other display objects.

This API supports loading from either a local file containing HTML content (in one of the system directories) or from a remote website.

Gotchas

Because native web views are not part of the OpenGL canvas, they do not obey the Corona display object hierarchy. For example, while they can be manipulated using display object methods, they always appear above normal display objects, and cannot be inserted into display groups.

General

This API should not be confused with the similar (older) native.showWebPopup().

You must close a web view by calling the removeSelf() method, because returning false in the listener function has no effect (with web popups, which are different from web views, returning false in the listener closes the web popup—this is not the case with native web views).

Native web views are available on iOS devices, Android devices, and the Corona Simulator for Mac. This feature is not supported by the Corona Simulator for Windows.

iOS

On iOS, when checking for event.errorMessage in a web view listener (see example below), you may occasionally receive an error message such as NSURLerrorDomain error -999. This is returned when an asynchronous load is cancelled. A Web Kit framework delegate will receive this error when it performs a cancel operation on a loading resource, however a NSURLConnection or NSURLDownload delegate will not receive this error if the download is cancelled. This event/error may occur if a link within web view is clicked while the web view is still in the process of loading the first page. Generally speaking, this error can be ignored.

Android

Native web views cannot be rotated on Android.

If the web view is displaying web pages from the Internet, then you must add an Internet permission to your "build.settings" file. This permission is unnecessary if the web view will only be displaying local HTML files that do not access the Internet.

settings =
{
    android =
    {
        usesPermissions =
        {
            "android.permission.INTERNET",
        },
    },
}

Syntax

native.newWebView( centerX, centerY, width, height )
centerX, centerY (required)

Numbers. The center x and y starting position of the web view.

width, height (required)

Numbers. The width/height of the web view object (in pixels).

Functions and Properties

See the WebView documentation for a list of functions and properties.

Examples

Basic Web View
local webView = native.newWebView( display.contentCenterX, display.contentCenterY, 320, 480 )
webView:request( "http://www.coronalabs.com/" )
-- or
webView:request( "localfile.html", system.ResourceDirectory )
Same + Response From Callback Listener
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( "http://www.coronalabs.com/" )

webView:addEventListener( "urlRequest", webListener )