Share consent with WebViews

If your mobile app sometimes opens WebViews or Chrome Custom Tabs to display specific content to the user, you should ensure that consent is passed from your app to the website loaded in the WebView or Chrome Custom Tab. That will ensure that the user does not have to give consent again in WebViews or Chrome Custom Tabs.

The setup is the following:

  • Collect consent in your app with the Didomi native SDKs

  • Launch a WebView or a Chrome Custom Tab with the Didomi Web SDK embedded in it

  • Inject JavaScript into the WebView to pass consent information from the native app

This guide assumes that you have already setup the Didomi native SDKs in your Android or iOS app, and that you have setup our Web SDK in the HTML pages loaded into your WebViews or Chrome Custom Tabs.

SDK requirements The minimum SDK versions that support passing TCF v2 consent to a WebView are 1.26.1 (Android) and 1.38.0 (iOS).

The minimum SDK version that supports passing consent to a Chrome Custom Tab is 1.17.0 (Android).

If your app is a web application embedded through a WebView or a PWA, we recommend using our Web SDK rather than our native mobile SDKs.

Web SDK configuration in the WebView or Chrome Custom Tab

You need to embed the Didomi Web SDK in the HTML page that is loaded by the WebView or the Chrome Custom Tab so that it can collect the consent information passed from the app and share it with vendors.

The list of vendors configured in the web SDK in the WebView should be a subset of the list of vendors configured in the mobile app. That will ensure that the WebView has all the consent information it needs and does not re-collect consent. Examples:

Configuration
WebView behavior

Vendors in the mobile app and the WebView are the same

WebView will not display the consent UI and will use the consent information provided by the mobile app as is

Vendors in the WebView are a subset of the vendors in the mobile app

WebView will not display the consent UI and will use the consent information provided by the mobile app as is

Vendors in the mobile app are a subset of the vendors in the WebView, or there is no vendor in common

WebView will display the consent UI and collect user consent for the vendors that are specific to the WebView

Other parameters of the web SDK can be configured freely, in particular with respect to the IAB framework and tags management.

Passing consent to a WebView or to a Chrome Custom Tab is a slightly different operation. Read below for specific instructions based on the method you are using.

WebViews (Android and iOS)

The consent status can be passed to the Web SDK by embedding JavaScript code into your WebView.

On Android, make sure JavaScript is enabled on the WebView. In some configurations (for example, when using a custom domain), enabling DOM storage is also necessary.

The Android and iOS SDKs automatically generate the required JavaScript code for you with the getJavaScriptForWebView method. Call that method and embed the returned string into your WebView:

webView.settings.javaScriptEnabled = true
webView.settings.domStorageEnabled = true

val didomi = Didomi.getInstance()
didomi.onReady {
    val didomiJavaScriptCode = didomi.getJavaScriptForWebView()
    webView.evaluateJavascript(didomiJavaScriptCode) { s ->
    }
}

Chrome Custom Tabs (Android only)

Android SDK

As Chrome Custom Tabs do not allow embedding JavaScript into a web page, the consent status of the user can be passed to the Web SDK by appending a query string parameter to the URL loaded by the Chrome Custom Tab. That query string parameter will contain the current status of the user and will be read by the Web SDK.

val didomi = Didomi.getInstance()
didomi.onReady {
    val didomiQueryString = didomi.queryStringForWebView
    val url = "https://www.website.com/?$didomiQueryString"
}

The getQueryStringForWebView method returns a query string parameter with the format didomiConfig.user.externalConsent.value=.... It can be appended to your URL after a ? or a & if your URL already contains a query string.

Example of a full URL: https://www.website.com/?didomiConfig.user.externalConsent.value=...

Web SDK

The Web SDK needs to be configured to read the user consent from the query string as that behavior is disabled by default. To do so, update your custom JSON or your local didomiConfig with:

{
  user: {
    externalConsent: {
      enabled: true
    }
  }
}

Hide the notice in the WebView or Chrome Custom Tab

The notice should automatically get hidden in the WebView as consent is passed from the mobile app to the website. However, it might happen that the notice gets displayed for a very short time before being hidden.

To avoid that visual glitch, you can disable the notice in your WebView or Chrome Custom Tab to make sure that it never shows by appending didomiConfig.notice.enable=false to the query string of the URL that you are loading:

{{YOUR_WEBSITE_URL}}?didomiConfig.notice.enable=false

If the notice still shows in the WebView or Chrome Custom Tab when navigating to another page, ensure that the list of vendors and purposes is the same between the mobile app and the website that is loaded in the WebView or Chrome Custom Tab.

Last updated