This article describes how to use the MoEngage Web SDK in WebView inside a Mobile App.
info |
Information
|
Steps to Enable WebSDK in WebView
To run WebSDK in WebView,
- Ensure you have correctly integrated Web SDK into your website.
- Ensure you provide permission to use JavaScript for your Android/iOS app.
For Android Apps
Step 1: Configure Your WebView
Within your app, locate the WebView
component for which you wish to enable JavaScript. Ensure you have the proper imports:
import android.webkit.WebView;
import android.webkit.WebSettings;
Step 2: Enable JavaScript and DomStorage Permissions
Using the WebSettings
class, enable JavaScript:
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true); // Enable DOM storage API
Step 3: Advanced Settings
For more control, you may use additional settings such as:
webSettings.setJavaScriptCanOpenWindowsAutomatically(true); // Allow JS to open windows without user interaction
info |
Information It is important to consider the security implications of enabling JavaScript. Always validate the URL you load in |
For iOS Apps
Step 1: Import WebKit
First, ensure you've imported the WebKit
framework in your ViewController:
import WebKit
Step 2: Create a WKWebView
Create an instance ofWKWebView
and add it to your view:
var webView: WKWebView!
webView = WKWebView(frame: .zero, configuration: WKWebViewConfiguration())
view.addSubview(webView)
Step 3: Enable JavaScript
JavaScript is enabled by default onWKWebView
, but you can verify or change the settings through the WKPreferences
object:
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
webView = WKWebView(frame: .zero, configuration: configuration)
Supported Modules
In WebView, we support the following modules:
- Data Tracking
- On-site Messaging
- Card's
- Web Personalization