Defold Webview API documentation

Functions for creating and controlling webviews to show HTML pages or evaluate JavaScript. These API:s only exist on mobile platforms.

To use this library in your Defold project, add the following URL to your game.project dependencies:

https://github.com/defold/extension-webview/archive/master.zip

The source code can be viewed at: https://github.com/defold/extension-webview


Modules

webview

Functions and constants for interacting with webview APIs

Enums

webview.CALLBACK_RESULT_URL_OK
webview.CALLBACK_RESULT_URL_ERROR
webview.CALLBACK_RESULT_URL_LOADING
webview.CALLBACK_RESULT_EVAL_OK
webview.CALLBACK_RESULT_EVAL_ERROR

Functions

webview.create() Creates a webview instance. It can show HTML pages as well as evaluate Javasc...
webview.destroy() Destroys an instance of a webview.
webview.open_raw() Opens a web page in the webview, using HTML data. Once the request is done, t...
webview.eval() Evaluates JavaScript within the context of the currently loaded page (if any)...
webview.set_visible() Shows or hides a webview
webview.is_visible() Returns the visibility state of the webview.
webview.set_position() Sets the position and size of the webview

webview.create(callback)

Parameter Type Description
callback function

A callback which receives info about finished requests taking the following parameters:

Parameter Type Description
self object

The calling script

webview_id number

The webview id

request_id number

The request id

type enum

The type of callback

  • webview.CALLBACK_RESULT_URL_OK
  • webview.CALLBACK_RESULT_URL_ERROR
  • webview.CALLBACK_RESULT_URL_LOADING
  • webview.CALLBACK_RESULT_EVAL_OK
  • webview.CALLBACK_RESULT_EVAL_ERROR
data table

A table holding the data

Creates a webview instance. It can show HTML pages as well as evaluate Javascript. The view remains hidden until the first call. There can exist a maximum of 4 webviews at the same time. On iOS, the callback will never get a webview.CALLBACK_RESULT_EVAL_ERROR, due to the iOS SDK implementation.”

Examples

local function webview_callback(self, webview_id, request_id, type, data)
    if type == webview.CALLBACK_RESULT_URL_OK then
        -- the page is now loaded, let's show it
        webview.set_visible(webview_id, 1)
    elseif type == webview.CALLBACK_RESULT_URL_ERROR then
        print("Failed to load url: " .. data["url"])
        print("Error: " .. data["error"])
    elseif type == webview.CALLBACK_RESULT_URL_LOADING then
        -- a page is loading
        -- return false to prevent it from loading
        -- return true or nil to continue loading the page
        if data.url ~= "https://www.defold.com/" then
            return false
        end
    elseif type == webview.CALLBACK_RESULT_EVAL_OK then
        print("Eval ok. Result: " .. data['result'])
    elseif type == webview.CALLBACK_RESULT_EVAL_ERROR then
        print("Eval not ok. Request # " .. request_id)
    end
end
local webview_id = webview.create(webview_callback)

webview.destroy(webview_id)

Parameter Type Description
webview_id number

The webview id (returned by the webview.create() call)

Destroys an instance of a webview.

webview.open_raw(webview_id, html, options)

Parameter Type Description
webview_id number

The webview id

html string

The HTML data to display

options table

A table of options for the request. See webview.open()

Opens a web page in the webview, using HTML data. Once the request is done, the callback (registered in webview.create()) is invoked.

Examples

local html = sys.load_resource("/main/data/test.html")
local request_id = webview.open_raw(webview_id, html, {hidden = true})

webview.eval(webview_id, code)

Parameter Type Description
webview_id number

The webview id

code string

The JavaScript code to evaluate

Evaluates JavaScript within the context of the currently loaded page (if any). Once the request is done, the callback (registered in webview.create()) is invoked. The callback will get the result in the data["result"] field.

Examples

local request_id = webview.eval(webview_id, "GetMyFormData()")

webview.set_visible(webview_id, visible)

Parameter Type Description
webview_id number

The webview id

visible number

If 0, hides the webview. If non zero, shows the view

Shows or hides a webview

webview.is_visible(webview_id)

Parameter Type Description
webview_id number

The webview id

Returns the visibility state of the webview.

webview.set_position(webview_id, x, y, width, height)

Parameter Type Description
webview_id number

The webview id

x number

The x position of the webview

y number

The y position of the webview

width number

The width of the webview (-1 to match screen width)

height number

The height of the webview (-1 to match screen height)

Sets the position and size of the webview