Open
Conversation
Related APIs:
- `webView:registerCallback(name, function)`: Register a Lua function that can be called from JavaScript
- `webView:on(eventName, listener)`: Listen for events from JavaScript
- `webView:send(eventName, data)`: Send data to WebView
- `webView:injectJS(script)`: Inject JavaScript code into WebView
How to use:
```lua
local myWebView = native.newWebView(
display.contentCenterX,
display.contentCenterY,
display.actualContentWidth,
display.actualContentHeight
)
webView:registerCallback("getDeviceInfo", function(data)
return {
platform = system.getInfo("platform"),
version = system.getInfo("architectureInfo"),
language = system.getPreference("locale", "language"),
deviceModel = system.getInfo("model")
}
end)
webView:on("buttonClicked", function(data)
print("Button clicked in WebView: " .. data.buttonId)
-- Respond to WebView
webView:send("buttonResponse", {message = "Received click for button " .. data.buttonId})
end)
local function injectJS()
webView:injectJS[[
function updateDeviceInfo() {
NativeBridge.callNative("getDeviceInfo").then(info => {
document.getElementById("deviceInfo").innerHTML =
`Platform: ${info.platform}<br>
Version: ${info.version}<br>
Language: ${info.language}<br>
Model: ${info.deviceModel}`;
});
}
document.getElementById("updateButton").addEventListener('click', function() {
updateDeviceInfo();
NativeBridge.sendToLua("buttonClicked", {buttonId: "update"});
});
document.getElementById("greetButton").addEventListener('click', function() {
NativeBridge.sendToLua("buttonClicked", {buttonId: "greet"});
});
NativeBridge.on("buttonResponse", function(data) {
document.getElementById("response").textContent = data.message;
});
]]
end
local function webListener( event )
if event.type == "loaded" then
injectJS()
end
end
webView:request( "index.html", system.ResourceDirectory )
webView:addEventListener( "urlRequest", webListener )
```
Here's an example HTML file that works with the above Lua and JavaScript code:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebView Example</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; }
button { margin: 10px 0; }
</style>
</head>
<body>
<h1>WebView Communication Example</h1>
<div id="deviceInfo"></div>
<button id="updateButton">Update Device Info</button>
<button id="greetButton">Send Greeting</button>
<p id="response"></p>
</body>
</html>
```
Now we will call window.onNativeBridgeLoaded on page loaded.
Use this feature in html:
```html
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<p>This is a simple HTML page.</p>
</body>
<script>
window.onNativeBridgeLoaded = function() {
// NativeBridge.callNative(method, args)
// NativeBridge.sendToLua(event, data)
// NativeBridge.on(event, callback, options)
}
</script>
</html>
```
…BridgeReady.
Now we will call window.onNativeBridgeReady on page loaded.
Use this feature in html:
```html
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<p>This is a simple HTML page.</p>
</body>
<script>
window.onNativeBridgeReady = function() {
// NativeBridge.callNative(method, args)
// NativeBridge.sendToLua(event, data)
// NativeBridge.on(event, callback, options)
}
</script>
</html>
```
|
This looks very handy! @Shchvova Can this be merged? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related APIs:
webView:registerCallback(name, function): Register a Lua function that can be called from JavaScriptwebView:on(eventName, listener): Listen for events from JavaScriptwebView:send(eventName, data): Send data to WebViewwebView:injectJS(script): Inject JavaScript code into WebViewSee more detail in this forum topic and this repo.