Skip to main content

JSCallNative

AndroidiOSWindows
If you were looking for the method for use with Unreal Engine, see JSCallNative for Unreal Engine SDK.

JS code calls Native (Android/iOS) functions through Player Network SDK WebView module to enable/disable fullscreen mode for webpages, open apps, perform other functions, and returns the result to INTLWebViewObserver.

For relevant codes, see INTLWebViewSample.html.

Call protocol

JS code and Native code communicate through JSON strings.

JSAPITypeDescription
promptstringJSON string
JSON parameterTypeDescription
INTLMethodstringCall function
INTLMethod parameterDescriptionAdditional parameter
closeWebViewClose the browser--
setFullScreenSet the browser to fullscreen mode"isFullScreen": true/false
setScreenOrientationSet the browser orientation"screenOrientation":"1"
1: Auto
2: Portrait
3: Landscape
OpenUrlInINTLBrowserOpen the specified URL with default system browser"url":"https://www.qq.com/"
jsCallNativeCall native code of the game--
goBackBack, only supports Windows platform--
ReportEventData reportingSee Input parameters of ReportEvent and the code sample [1]
shareWebViewShareSee Data structure of Share and the code sample [2]
clearWebViewFocusClear WebView focus--

iOS and Android Format sample:

{"INTLMethod":"OpenUrlInINTLBrowser","ParamKey":{"url":"https://www.qq.com"}}
{"INTLMethod":"setScreenOrientation","screenOrientation":"1"}
[1]
{"INTLMethod":"ReportEvent","eventName":"Login","key1":"","value1":"","key2":"","value2":"","spChannel":"","extraJson":""}
[2]
{"INTLMethod":"shareWebView","channel":"Facebook","type":10001,"user":"","title":"","desc":"","imagePath":"","thumbPath":"","mediaPath":"","link":"https://www.facebook.com","extraJson":""}

Windows format sample:

INTLWebView:

window.tbs_external.nativeCall("setFullScreen", ['{"INTLMethod":"setFullScreen","isFullScreen":true}']);
window.tbs_external.nativeCall("closeWebView", ['{"INTLMethod":"closeWebView"}']);

INTLWebView2:

window.chrome.webview.postMessage('{"INTLMethod":"setFullScreen","isFullScreen":true}');
window.chrome.webview.postMessage('{"INTLMethod":"closeWebView"}');

The keyword must include INTLMethod to specify the call method. Other fields are input parameters.

INTLMethod, channel, media detail, are used to specify call method, channel, and multimedia details, respectively.

Code sample

For the web client, Player Network SDK provides code samples of web client initialization, browser operation, and key media sharing/sending function.

Initialization code sample

<script>
// Get WebViewJavascriptBridge,iOS
function setupWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
window.WVJBCallbacks = [callback];
var WVJBIframe = document.createElement('iframe');
WVJBIframe.style.display = 'none';
WVJBIframe.src = 'https://__bridge_loaded__';
document.documentElement.appendChild(WVJBIframe);
setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
}
// Initialize intliOSHandler,iOS
setupWebViewJavascriptBridge(function(bridge) {
intliOSHandler = bridge.callHandler
bridge.registerHandler('INTLJSHandler', function(data, responseCallback) {
log('ObjC called INTLJSHandler with', data)
alert(data)
})
bridge.registerHandler('OnEnterForeground', function(data, responseCallback) {
OnEnterForeground()
})

bridge.registerHandler('OnEnterBackground', function(data, responseCallback) {
OnEnterBackground()
})

bridge.registerHandler('OnGetNetworkType', function(data, responseCallback) {
OnGetNetworkType(data)
})

bridge.registerHandler('OnOpenPhoto', function(data, responseCallback) {
OnOpenPhoto(data)
})
})
// Player Network SDK JS Test API
function INTLCallNative(data) {
console.log('INTLCallNative invoked start ->');
if (isiOS()) {
intliOSHandler('INTLCallNative', data, null)
} else {
if (data.indexOf("nativeCallJS") > -1) {
console.log('INTLCallNative invoked nativeCallJS : ' + data);
alert(data)
} else {
console.log('INTLCallNative invoked : ' + data);
prompt(data);
}
}
}
</script>

Browser operation code sample
Includes closing the browser, setting the browser to fullscreen mode, and restoring the browser from fullscreen mode.

//Initialization protocol parameters
<script>
var CloseWebView = '{"INTLMethod":"closeWebView"}';
var SetFullScreen = '{"INTLMethod":"setFullScreen","isFullScreen":true}';
var ResetFullScreen = '{"INTLMethod":"setFullScreen","isFullScreen":false}';
</script>

//Call INTLCallNative to trigger native from js
<input type="button" value="Close WebView" onclick="INTLCallNative(CloseWebView)">
<input type="button" value="Set WebView full screen" onclick="INTLCallNative(SetFullScreen)">
<input type="button" value="Reset WebView full screen" onclick="INTLCallNative(ResetFullScreen)">