Hybrid App Setup - Unity SDK

Note

Before proceeding with the hybrid app setup, install the Airbridge Unity SDK and Web SDK.

You can configure the Airbridge Unity SDK to handle Airbridge-related tasks occurring within the in-app website of a hybrid app without modifying the website's code.

Handle Web SDK Commands with the Unity SDK

The Unity SDK can handle commands related to event transmission, device settings, and user settings that occur within the Web SDK. Before displaying the website in the web view, configure the Unity SDK using the Airbridge.CreateWebInterfaceScript and Airbridge.HandleWebInterfaceCommand functions.

  • webToken: This is the Web SDK token. Your Web SDK token can be found on the [Settings]>[Tokens] page in the Airbridge dashboard.

  • postMessageScript: This JavaScript code transmits the payload variable, which stores the command passed from the Web SDK to the Unity SDK, to the Unity area.

  • command: This command is passed from the Web SDK to the Unity SDK.

123456789101112131415161718192021222324252627282930313233343536373839404142
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://...");
}
123456789101112131415161718192021222324252627282930313233343536373839
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();  
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
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;
    }
}

Attention

Note that with the hybrid app setup, events that occur in the in-app web view must be collected through the Web SDK only. If collected through the App SDK as well, event duplication will occur. If your in-app web view uses a live mobile website, utilize only the Web SDK for event collection.