알립니다
하이브리드 앱에서 웹 사이트 코드를 변경하지 않아도 에어브릿지 유니티 SDK가 인앱 웹 사이트에서 발생하는 에어브릿지 관련 작업을 처리하도록 설정할 수 있습니다.
유니티 SDK는 웹 SDK에서 진행되는 이벤트 전송, 디바이스 설정, 유저 설정에 관한 명령을 대신 처리할 수 있습니다. 웹뷰에 웹 사이트를 표시하기 전에 Airbridge.CreateWebInterfaceScript 함수와 Airbridge.HandleWebInterfaceCommand 함수를 이용해 설정해 주세요.
webToken 은 웹 SDK 토큰입니다. 웹 SDK 토큰은 에어브릿지 대시보드의 [설정]>[토큰 관리]에서 확인할 수 있습니다.
postMessageScript 는 웹 SDK가 유니티 SDK에 전달하는 명령어가 저장된 payload 변수를 유니티 영역에 전달하는 자바스크립트 코드입니다.
command 는 웹 SDK가 유니티 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; }}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; }}아래 사항을 주의해 주세요.
하이브리드 앱 설정을 하면 웹 SDK만으로 인앱 웹뷰에서 발생하는 이벤트를 수집해야 합니다. 앱 SDK로 이벤트를 추가로 수집하면 해당 이벤트가 중복해서 집계됩니다.
인앱 웹뷰에 실제 모바일 웹사이트를 사용하면 웹 SDK만으로 이벤트를 수집해 주세요.
도움이 되었나요?