- Select ref document -

Webview API documentation

version 1.2.144

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

Functions

webview.create

webview.create(callback)

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.

Parameters

callback

function(self, webview_id, request_id, type, data) A callback which receives info about finished requests taking the following parameters

self
object The calling script
webview_id
number The webview id
request_id
number The request id
type
number The type of the callback. Can be one of these:
  • webview.CALLBACK_RESULT_URL_OK
  • webview.CALLBACK_RESULT_URL_ERROR
  • webview.CALLBACK_RESULT_EVAL_OK
  • webview.CALLBACK_RESULT_EVAL_ERROR
data
table A table holding the data. The table has these fields:
  • string url: The url used in the webview.open() call. nil otherwise.
  • string result: Holds the result of either: a failed url open, a successful eval request or a failed eval. nil otherwise
callback

function(self, webview_id, request_id, type, data) A callback which receives info about finished requests taking the following parameters

self
object The calling script
webview_id
number The webview id
request_id
number The request id
type
number The type of the callback. Can be one of these:
  • webview.CALLBACK_RESULT_URL_OK
  • webview.CALLBACK_RESULT_URL_ERROR
  • webview.CALLBACK_RESULT_EVAL_OK
  • webview.CALLBACK_RESULT_EVAL_ERROR
data
table A table holding the data. The table has these fields:
  • string url: The url used in the webview.open() call. nil otherwise.
  • string result: Holds the result of either: a failed url open, a successful eval request or a failed eval. nil otherwise

Returns

id

number The id number of the webview

id

number The id number of the webview

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_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.destroy(webview_id)

Destroys an instance of a webview.

Parameters

webview_id

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

webview_id

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

webview.eval

webview.eval(webview_id, code)

Evaluates java script 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.

Parameters

webview_id

number The webview id

code

string The java script code to evaluate

webview_id

number The webview id

code

string The java script code to evaluate

Returns

id

number The id number of the request

id

number The id number of the request

Examples

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

webview.is_visible

webview.is_visible(webview_id)

Returns the visibility state of the webview.

Parameters

webview_id

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

webview_id

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

Returns

visibility

number Returns 0 if not visible, 1 if it is visible

visibility

number Returns 0 if not visible, 1 if it is visible

webview.open

webview.open(webview_id, url, options)

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

Parameters

webview_id

number The webview id

url

string The url to open

options

table A table of options for the request. Currently it holds these options

hidden
boolean If true, the webview will stay hidden (default=false)
webview_id

number The webview id

url

string The url to open

options

table A table of options for the request. Currently it holds these options

hidden
boolean If true, the webview will stay hidden (default=false)

Returns

id

number The id number of the request

id

number The id number of the request

Examples

local request_id = webview.open(webview_id, "http://www.defold.com", {hidden = true})

webview.open_raw

webview.open_raw(webview_id, html, options)

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

Parameters

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()

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()

Returns

id

number The id number of the request

id

number The id number of the request

Examples

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

webview.set_position

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

Sets the position and size of the webview

Parameters

webview_id

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

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)

webview_id

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

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)

webview.set_visible

webview.set_visible(webview_id, visible)

Shows or hides a web view

Parameters

webview_id

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

visible

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

webview_id

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

visible

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