# Webviews with Web SDK 2.1.0+ on Android source: https://developer.mastercard.com/open-finance-us/documentation/connect/integrating/sdk/webviews/android-webviews-2-1-0/index.md Note: For Web SDK versions before 2.1.0, use the [earlier Android WebView guide](https://developer.mastercard.com/open-finance-us/documentation/connect/integrating/sdk/webviews/android-webviews/index.md). This page describes how to load your web app inside your Android mobile app using a WebView via the Mastercard Data Connect Web SDK (version 2.1.0 or later). This guide covers OAuth authentication, app links and deep links. The Data Connect flow follows these steps: 1. Your Android app launches your web app (integrate with the Connect Web SDK) in a WebView. 2. The user selects an option or link to launch Data Connect in the WebView (this option should have the `redirectUrl` code integrated already). 3. After the user adds accounts through OAuth, control returns to your app's activity using the app link or deep link `redirectUrl` declared in `AndroidManifest.xml`. Your app must handle both of these authentication scenarios: * If the financial institution's app is installed, authentication takes place in that app. * If the financial institution's app is not installed, OAuth login opens in a Chrome Custom Tab. Note: To support app to app authentication, pass a `redirectUrl` to the Data Connect Web SDK. For configuration details, refer to [App to App Support](https://developer.mastercard.com/open-finance-us/documentation/connect/integrating/sdk/web-sdk/sdk/index.md#app-to-app-support). ## Project Setup {#project-setup} Add the browser dependency to your app-level Gradle file, `build.gradle`: ```groovy dependencies { // ... implementation 'androidx.browser:browser:' } ``` For the latest dependency version, refer to the [Chrome Custom Tabs documentation](https://developer.chrome.com/docs/android/custom-tabs/guide-get-started). Add the `INTERNET` permission to `AndroidManifest.xml` so the WebView can access the internet. ```xml ..... ``` ## Layout Configuration {#layout-configuration} Include the `WebView` component in your layout XML file. ```xml ``` ## WebView Initialization {#webview-initialization} In your `Activity` class, initialize the `WebView` and JavaScript interface: * Java * Kotlin ```java public class YourActivity extends AppCompatActivity { private WebView webView; private Context context; private static ConnectJsInterface jsInterface; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_webview_layout); // Replace "your_webview_layout" with your own WebView layout name. context = this; webView = findViewById(R.id.webView); // Enable JavaScript and configure WebView settings webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setAllowFileAccess(true); webView.getSettings().setSupportMultipleWindows(true); // Set WebViewClient to handle URL loading webView.setWebViewClient(new ConnectWebViewClient()); // Initialize JavascriptInterface jsInterface = new ConnectJsInterface(this); webView.addJavascriptInterface(jsInterface, "acmeHandler"); //Replace "acmeHandler" with your own JavaScript interface name. // Load the Connect WebSDK URL webView.loadUrl("Pass Your Connect WebSDK integrated URL"); } } ``` ```kotlin class YourActivity : AppCompatActivity() { private lateinit var webView: WebView private lateinit var context: Context private lateinit var jsInterface: ConnectJsInterface override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.your_webview_layout) // Replace your_webview_layout with your WebView layout name. context = this webView = findViewById(R.id.webView) // Enable JavaScript and configure WebView settings with(webView.settings) { javaScriptEnabled = true allowFileAccess = true supportMultipleWindows = true } // Set WebViewClient to handle URL loading webView.webViewClient = ConnectWebViewClient() // Initialize JavascriptInterface jsInterface = ConnectJsInterface(this) webView.addJavascriptInterface(jsInterface, "acmeHandler") // Replace with your handler name // Load the Connect WebSDK URL webView.loadUrl("Pass Your Connect WebSDK integrated URL") } } ``` ## Loading Your Web App In the WebView {#loading-your-web-app-in-the-webview} Use `ConnectWebViewClient`, which extends `WebViewClient`, to handle URL loading. Customize the `shouldOverrideUrlLoading` method within `ConnectWebViewClient` to control how the main WebView loads URLs. * Java * Kotlin ```java // this class handles the loading of the urls inside the WebView. private class ConnectWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { String url = request.getUrl().toString(); view.loadUrl(url); return true; } } ``` ```kotlin // this class handles the loading of the urls inside the WebView. private inner class ConnectWebViewClient : WebViewClient() { override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { val url = request?.url.toString() view?.loadUrl(url) return true } } ``` ## OAuth and App to App Authentication {#oauth-and-app-to-app-authentication} Implement `ConnectJsInterface` to receive FI OAuth events through its `postMessage` JavaScript interface. If the FI app is not present, handle these `postMessage` events: * `OPEN`: Call `openLinkInCustomTab` to open the OAuth URL in a Chrome Custom Tab. * `CLOSE`: Call `closeCustomTab` to close the tab. For app to app authentication, pass your return URL as `redirectUrl` to the Data Connect Web SDK. Configure your Android activity to handle that app link or deep link in `AndroidManifest.xml`. For configuration details, refer to [App Link Support](https://developer.mastercard.com/open-finance-us/documentation/connect/integrating/sdk/android/android-sdk/index.md#app-link-support). When authentication finishes, the Chrome Custom Tab closes and control returns to your app's activity which runs Connect in the background with an add accounts screen. * Java * Kotlin ```java class ConnectJsInterface { private Activity activity; private Boolean mCustomTabStarted = false; public ConnectJsInterface(Activity activity) { this.activity = activity; } @JavascriptInterface public void postMessage(String message) { JSONObject jsonMessage; String eventType; JSONObject jsonDataMessage; try { jsonMessage = new JSONObject(message); jsonDataMessage = jsonMessage.getJSONObject("data"); eventType = jsonDataMessage.getString("type"); } catch(Exception e) { return; } if(eventType.equals("OPEN")) { try { String url = jsonDataMessage.getString("url"); openLinkInCustomTab(url); } catch (JSONException e) { } } else if(eventType.equals("CLOSE")) { closeCustomTab(); } } public void openLinkInCustomTab(String url) { CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); CustomTabsIntent customTabsIntent = builder.build(); Intent intent = customTabsIntent.intent; intent.setData(Uri.parse(url)); mCustomTabStarted = true; activity.startActivity(CustomTabsActivityManager.createStartIntent(activity, intent)); } public void closeCustomTab() { if (!mCustomTabStarted) { return; } mCustomTabStarted = false; activity.startActivity(CustomTabsActivityManager.createDismissIntent(activity)); } } ``` ```kotlin class ConnectJsInterface(private val activity: Activity) { private var mCustomTabStarted: Boolean = false @JavascriptInterface fun postMessage(message: String) { var jsonMessage: JSONObject? var eventType: String? = null var jsonDataMessage: JSONObject? = null try { jsonMessage = JSONObject(message) jsonDataMessage = jsonMessage.getJSONObject("data") eventType = jsonDataMessage.getString("type") } catch (e: Exception) { return } when (eventType) { "OPEN" -> { try { val url = jsonDataMessage.getString("url") openLinkInCustomTab(url) } catch (e: JSONException) { // Handle exception if needed } } "CLOSE" -> closeCustomTab() } } private fun openLinkInCustomTab(url: String) { val builder = CustomTabsIntent.Builder() val customTabsIntent = builder.build() val intent = customTabsIntent.intent intent.data = Uri.parse(url) mCustomTabStarted = true activity.startActivity(CustomTabsActivityManager.createStartIntent(activity, intent)) } private fun closeCustomTab() { if (!mCustomTabStarted) { return } mCustomTabStarted = false activity.startActivity(CustomTabsActivityManager.createDismissIntent(activity)) } } ``` Create a `CustomTabsActivityManager` activity to open and close Chrome Custom Tabs. This activity handles the intents for opening and dismissing a tab. * Java * Kotlin ```java import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; public class CustomTabsActivityManager extends Activity { static final String KEY_BROWSER_INTENT = "browserIntent"; private boolean mOpened = false; public static Intent createStartIntent(Context context, Intent customTabsIntent) { Intent intent = createBaseIntent(context); intent.putExtra(KEY_BROWSER_INTENT, customTabsIntent); return intent; } public static Intent createDismissIntent(Context context) { Intent intent = createBaseIntent(context); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); return intent; } private static Intent createBaseIntent(Context context) { return new Intent(context, CustomTabsActivityManager.class); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // This activity gets opened in 2 different ways. If the extra KEY_BROWSER_INTENT is present we // start that intent and if it is not it means this activity was started with FLAG_ACTIVITY_CLEAR_TOP // in order to close the intent that was started previously so we just close this. if (getIntent().hasExtra(KEY_BROWSER_INTENT)) { Intent browserIntent = getIntent().getParcelableExtra(KEY_BROWSER_INTENT); browserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(browserIntent); } else { finish(); } } @Override protected void onResume() { super.onResume(); // onResume will get called twice, the first time when the activity is created and a second // time if the user closes the chrome tabs activity. Knowing this we can detect if the user // dismissed the activity and send an event accordingly. if (!mOpened) { mOpened = true; } else { finish(); } } @Override protected void onDestroy() { super.onDestroy(); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); } } ``` ```kotlin import android.app.Activity import android.content.Context import android.content.Intent import android.os.Bundle class CustomTabsActivityManager : Activity() { companion object { private const val KEY_BROWSER_INTENT = "browserIntent" fun createStartIntent(context: Context, customTabsIntent: Intent): Intent { val intent = createBaseIntent(context) intent.putExtra(KEY_BROWSER_INTENT, customTabsIntent) return intent } fun createDismissIntent(context: Context): Intent { val intent = createBaseIntent(context) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) return intent } private fun createBaseIntent(context: Context): Intent { return Intent(context, CustomTabsActivityManager::class.java) } } private var mOpened = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // This activity gets opened in 2 different ways. If the extra KEY_BROWSER_INTENT is present we // start that intent and if it is not it means this activity was started with FLAG_ACTIVITY_CLEAR_TOP // in order to close the intent that was started previously so we just close this. if (intent.hasExtra(KEY_BROWSER_INTENT)) { val browserIntent: Intent? = intent.getParcelableExtra(KEY_BROWSER_INTENT) browserIntent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) browserIntent?.let { startActivity(it) } } else { finish() } } override fun onResume() { super.onResume() // onResume will get called twice, the first time when the activity is created and a second // time if the user closes the chrome tabs activity. Knowing this we can detect if the user // dismissed the activity and send an event accordingly. if (!mOpened) { mOpened = true } else { finish() } } override fun onDestroy() { super.onDestroy() } override fun onNewIntent(intent: Intent?) { super.onNewIntent(intent) setIntent(intent) } } ``` Add the `CustomTabsActivityManager` activity to `AndroidManifest.xml`: ```xml ```