Use the Javascript-Widget within a web-view in your mobile app.
You can also integrate our Javascript-Widget solution inside a web-view in your mobile App(s).
The integration follows almost the same procedure as described in our description for Javascript-Widget.
Some special steps have to be taken when integrating the JS-Widget in a web-view, which will be described below in the examples.
Implementation Steps for the Hosted Page Implementing the JS-Widget
You will need a HTML page that implements the JS-Widget, just like described in Javascript-Widget. In addition, the following changes are recommended:
HTML
<!-- in the <head> -->
<!-- Scale settings so the page does not zoom/move-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no, maximum-scale=1.0, user-scalable=no">
CSS
#XS2A-Forminput:not([type='checkbox']):not([type='radio']),#XS2A-Formselect {/* Increase the font size for input elements, 16px minimum is recommended */font-size:16px!important;}
JS-to-Native Callbacks
The most important steps to be taken are about handling the callbacks the JS-Widget does in your mobile app. For example, when the user finishes the process, you probably want to close the web-view and show the user a specific screen. The same needs to be done when the user aborts.
This can be accomplished with registering callbacks, that notify the host app (your app) from within the Javascript on the hosted page. You might already have callbacks defined such as xs2a.finish() or xs2a.abort() , for which you will need to add some more functionality:
xs2a.finish(function () {if (window.App) {// Callback to native Android AppApp.finishTransaction(); } elseif (window.webkit) {// Callback to native iOS Appwebkit.messageHandlers.callback.postMessage("finished"); }});xs2a.abort(function () {if (window.App) {// Callback to native Android AppApp.abortTransaction(); } elseif (window.webkit) {// Callback to native iOS Appwebkit.messageHandlers.callback.postMessage("aborted"); }});xs2a.configure({// ESSENTIAL!// this needs to be set so that in-app webviews handle redirects properly'reuse-tab-on-redirect':true});
Implementation Steps for the Native Apps
In the app, we need some functionality to show the web-view and to deal with the callbacks defined above.
iOS
The following is an example class showing an integration with WKWebView and how the JS-to-Native callbacks are defined and configured:
let vc =WebViewController(url:<URL_TO_HOSTED_PAGE>)self.present(vc, animated:true, completion:nil)
Android
Register the JS-to-Native callbacks:
importandroid.util.Log;importandroid.webkit.JavascriptInterface;importandroid.widget.Toast;importandroidx.navigation.fragment.NavHostFragment;publicclassWebViewJSInterface {privatestaticfinalString TAG ="WebViewJSInterface";SecondFragment fragment;publicWebViewJSInterface(SecondFragment fragment) {this.fragment= fragment; } @JavascriptInterfacepublicvoidfinishTransaction() {Log.d(TAG,"finishTransaction: Finish Transaction on Android");// navigate to success screen } @JavascriptInterfacepublicvoidabortTransaction() {Log.d(TAG,"finishTransaction: Abort Transaction on Android");// navigate to e.g. abort screen }}
Configure and present the web-view:
privatevoidloadXS2AIntoWebView(@NonNullView view,String wizardSessionKey) {WebView webView =view.findViewById(R.id.main_web_view);webView.getSettings().setJavaScriptEnabled(true);// This lets use use window.App in the JSwebView.addJavascriptInterface(newWebViewJSInterface(this),"App");webView.setWebViewClient(newWebViewClient() {publicbooleanshouldOverrideUrlLoading(WebView view,String url) {view.loadUrl(url);returntrue; } });webView.loadUrl(<URL_TO_HOSTED_PAGE>);}