提示
通过设置,您无需更改 Hybrid App 中网站的代码,也能让 Airbridge Unity SDK 处理应用内网站中的 Airbridge 相关操作。
Unity SDK 可以代替 Web SDK 处理事件发送、设备设置和用户设置等命令。在 WebView 中加载网站之前,请使用 Airbridge.CreateWebInterfaceScript
和 Airbridge.HandleWebInterfaceCommand
函数进行设置。
webToken
是 Web SDK Token,可在 Airbridge 面板的 [设置]>[Token] 获取。
postMessageScript
是一段 JavaScript 代码,用于将 Web SDK 传递给 Unity SDK 的命令所存储的 payload
变量传递至 Unity 端。
command
是 Web SDK 传递给 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;
}
}
启用 Hybrid App 设置后,应仅通过 Web SDK 收集在应用内 WebView 中发生的事件。如果同时使用 App SDK 收集事件,可能会导致重复统计。
如果应用内 WebView 使用的是实际的移动版网站,请仅通过 Web SDK 收集事件。
Was this page helpful?
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;
}
}