Notice
You can set the Unity SDK to handle Airbridge related tasks occurring on the in-app website in hybrid apps without changing the website code.
Unity SDK can handle commands related to event transmission, device settings, and user settings that take place in the Web SDK. Set up using Airbridge.CreateWebInterfaceScript
and Airbridge.HandleWebInterfaceCommand
functions before displaying the website in the web view.
webToken
is a Web SDK token. The Web SDK token can be checked in Airbridge dashboard under [Settings]>[Tokens].
postMessageScript
is a JavaScript code that delivers the payload variable, which stores the command passed from the web SDK to the Unity SDK, to the Unity area.
command
is the command received from the web SDK delivered to the Unity SDK.
WebViewObject webViewObject;
string postMessageScript;
public void Display()
{
string PostMessageGenerator(string arg) =>
$@"
if (window && window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.unityControl) {{
window.webkit.messageHandlers.unityControl.postMessage({arg});
}} else {{
var iframe = document.createElement('IFRAME');
iframe.setAttribute('src', 'unity:' + {arg});
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
}}";
postMessageScript = Airbridge.CreateWebInterfaceScript("YOUR_WEB_SDK_TOKEN", PostMessageGenerator("payload"));
webViewObject.Init(
cb: (msg) =>
{
// do something
// Allow the Airbridge Unity SDK to process the forwarded messages from the WebView.
string command = msg;
Airbridge.HandleWebInterfaceCommand(command);
},
err: (msg) => { /* do something */ },
httpErr: (msg) => { /* do something */ },
started: (msg) => { /* do something */ },
hooked: (msg) => { /* do something */ },
ld: (msg) =>
{
// do something
webViewObject.EvaluateJS(postMessageScript);
}
);
webViewObject.LoadURL("https://...");
}
UniWebView webView;
string postMessageScript;
public void Display()
{
string PostMessageGenerator(string arg) => $@"window.location.href = 'uniwebview://airbridge?command=' + {arg}";
postMessageScript = Airbridge.CreateWebInterfaceScript("YOUR_WEB_SDK_TOKEN", PostMessageGenerator("payload"));
webView.OnMessageReceived += (view, message) =>
{
if (message.Path.Equals("airbridge")) {
string command = message.Args["command"];
Airbridge.HandleWebInterfaceCommand(command);
}
};
webView.OnPageFinished += (view, statusCode, url) =>
{
// do something
webView.EvaluateJavaScript(postMessageScript, (payload) =>
{
if (payload.resultCode.Equals("0"))
{
Debug.Log("Airbridge javascript injection succeeded");
}
else
{
Debug.Log("Airbridge javascript injection failed: " + payload.data);
}
});
};
// ...
webView.Load("https://...");
webView.Show();
}
string postMessageScript;
public void Display()
{
string PostMessageGenerator(string arg) => $@"window.location.href = 'airbridge://?command=' + {arg}";
postMessageScript = Airbridge.CreateWebInterfaceScript("YOUR_WEB_SDK_TOKEN", PostMessageGenerator("payload"));
GpmWebView.ShowUrl(
"https://...",
new GpmWebViewRequest.Configuration() { /* ... */ },
OnCallback,
new List<string>()
{
"airbridge" // PUT AIRBRIDGE SCHEME HERE
}
);
}
private void OnCallback(
GpmWebViewCallback.CallbackType callbackType,
string data,
GpmWebViewError error
)
{
switch (callbackType)
{
// ...
case GpmWebViewCallback.CallbackType.PageLoad:
if (string.IsNullOrEmpty(data) == false)
{
// do something
GpmWebView.ExecuteJavaScript(postMessageScript);
}
break;
case GpmWebViewCallback.CallbackType.Scheme:
if (error == null)
{
Uri uri = new Uri(data);
if (uri.Scheme.Equals("airbridge"))
{
try
{
string encodedCommand = HttpUtility.ParseQueryString(uri.Query)["command"];
string command = HttpUtility.UrlDecode(encodedCommand);
Airbridge.HandleWebInterfaceCommand(command);
}
catch (Exception e)
{
Console.WriteLine("Fail to receive command. Error: {0}", e.Message);
}
}
}
else
{
Debug.Log(string.Format("Fail to custom scheme. Error: {0}", error));
}
break;
}
}
Beware following.
Make sure to collect the events that occur in the in-app WebView through the Web SDK in a hybrid app setting. When collecting such events through the App SDK, event duplication will occur.
このページは役に立ちましたか?
string postMessageScript;
public void Display()
{
string PostMessageGenerator(string arg) => $@"window.location.href = 'airbridge://?command=' + {arg}";
postMessageScript = Airbridge.CreateWebInterfaceScript("YOUR_WEB_SDK_TOKEN", PostMessageGenerator("payload"));
GpmWebView.ShowUrl(
"https://...",
new GpmWebViewRequest.Configuration() { /* ... */ },
OnCallback,
new List<string>()
{
"airbridge" // PUT AIRBRIDGE SCHEME HERE
}
);
}
private void OnCallback(
GpmWebViewCallback.CallbackType callbackType,
string data,
GpmWebViewError error
)
{
switch (callbackType)
{
// ...
case GpmWebViewCallback.CallbackType.PageLoad:
if (string.IsNullOrEmpty(data) == false)
{
// do something
GpmWebView.ExecuteJavaScript(postMessageScript);
}
break;
case GpmWebViewCallback.CallbackType.Scheme:
if (error == null)
{
Uri uri = new Uri(data);
if (uri.Scheme.Equals("airbridge"))
{
try
{
string encodedCommand = HttpUtility.ParseQueryString(uri.Query)["command"];
string command = HttpUtility.UrlDecode(encodedCommand);
Airbridge.HandleWebInterfaceCommand(command);
}
catch (Exception e)
{
Console.WriteLine("Fail to receive command. Error: {0}", e.Message);
}
}
}
else
{
Debug.Log(string.Format("Fail to custom scheme. Error: {0}", error));
}
break;
}
}