Skip to content

Presenting the CMP

  1. On your UserOptions object, enable predefinedUI.
    let userOptions = UserOptions(..., predefinedUI: true, ...)
    
  2. Request an optional UIViewController or UINavigationController, by calling getPredefinedUI() from an initialized instance of the Usercentrics class and provide a dismiss block. The dismiss block depends on the presentation way.

    var predefinedUI: UIViewController?
    predefinedUI = usercentrics.getPredefinedUI(settings: nil) { ... }
    
    var predefinedUI: UINavigationController?
    predefinedUI = usercentrics.getPredefinedUI(settings: nil) { ... }
    
    var predefinedUI: UIViewController?
    predefinedUI = usercentrics.getPredefinedUI(settings: nil) { ... }
    
  3. Present the CMP and provide a dismiss block

    var predefinedUI: UIViewController?
    predefinedUI = usercentrics?.getPredefinedUI(settings: nil) {
        self.predefinedUI?.dismiss(animated: true, completion: nil)
    }
    
    guard let ui = self.predefinedUI else { return }
    self.navigationController?.present(ui, animated: true, completion: nil)
    
    Presenting modally in iOS 13 or later
    In most cases, you do not want to allow users to dismiss the CMP with a swipe down, if your UIViewController is presented modally in iOS 13 or later.
    You can disable this feature with the following line of code:
    
    guard let ui = self.predefinedUI else { return }
    if #available(iOS 13.0, *) { ui.isModalInPresentation = true }
    self.navigationController?.present(ui, animated: true, completion: nil)
    
    Modal Presentation Styles
    Take advantage of the `modalPresentationStyle` property in order to present your UI in different styles:
    
    guard let ui = self.predefinedUI else { return }
    ui.modalPresentationStyle = .overFullScreen
    self.navigationController?.present(ui, animated: true, completion: nil)
    
    var predefinedUI: UIViewController?
    predefinedUI = usercentrics?.getPredefinedUI(settings: nil) {
        self.navigationController?.popViewController(animated: true)
    }
    
    guard let ui = self.predefinedUI else { return }
    self.navigationController?.pushViewController(ui, animated: true)
    
    var predefinedUI: UINavigationController?
    predefinedUI = usercentrics?.getPredefinedUI(settings: nil) {
        self.predefinedUI?.dismiss(animated: true, completion: nil)
    }
    
    guard let ui = self.predefinedUI else { return }
    self.navigationController?.present(ui, animated: true, completion: nil)
    
    Presenting modally in iOS 13 or later
    In most cases, you do not want to allow users to dismiss the CMP with a swipe down, if your UIViewController is presented modally in iOS 13 or later.
    You can disable this feature with the following line of code:
    
    guard let ui = self.predefinedUI else { return }
    if #available(iOS 13.0, *) { ui.isModalInPresentation = true }
    self.navigationController?.present(ui, animated: true, completion: nil)
    
    Modal Presentation Styles
    Take advantage of the `modalPresentationStyle` property in order to present your UI in different styles:
    
    guard let ui = self.predefinedUI else { return }
    ui.modalPresentationStyle = .overFullScreen
    self.navigationController?.present(ui, animated: true, completion: nil)
    

Dismissing the CMP

The dismiss block provided will be called once the CMP has collected and is done saving consent. Depending on how you present your CMP, you will want to provide the according dismiss function to update your layout/view.

This completition block will be called after each of the following actions:

  • Accept All
  • Denny All
  • Save Settings
  • Close Button

There are two solutions to integrate the Predefined UI in Android:

  • The Activity. A ready-to-use Android Activity made by us.

  • The View. A View instance that you can add to your layout programmatically. You gain more control but it requires more code.

This approach follow the official Android guidelines.

In order to use it, the following steps are needed:

  1. On your UserOptions object, enable predefinedUI.
    val userOptions = UserOptions(..., predefinedUI = true, ...)
    
  2. Register UsercentricsActivity in your fragment or activity where you want to use it. Save the ActivityResultLauncher in a member variable in order to use it later.
    private val usercentricsActivityLauncher = registerForActivityResult(UsercentricsActivityContract()) { services ->
        // ...
    }
    
  3. Launch UsercentricsActivity.

    val arguments = UsercentricsActivityArguments(
        settingsID = "xXxxxXXxX",
        userOptions = userOptions
    )
    usercentricsActivityLauncher.launch(arguments)
    

    What to do in projects without AndroidX and old Android API levels?

    You have to invoke manually startActivityForResult and process the result in the onActivityResult. You can still use UsercentricsActivityContract for that purpose:

    val intent = UsercentricsActivityContract().createIntent(context, arguments)
    startActivityForResult(intent, requestCode)
    

In order to use it, the following steps are needed:

  1. On your UserOptions object, enable predefinedUI.

    val userOptions = UserOptions(..., predefinedUI = true, ...)
    

  2. Request a nullable View, by calling getPredefinedUI() from an initialized instance of the Usercentrics class and provide a dismiss block. The layout object is the ViewGroup where you want to add the View.

    var predefinedUI: View? = null
    predefinedUI = usercentrics.getPredefinedUI(viewContext = myActivity) {
        layout.removeView(this.predefinedUI)
        predefinedUI = null
        ...
    }
    

  3. Present the View

    layout.addView(predefinedUI)
    

  4. In order to provide a fully proper navigation and avoid dismissing the view before saving the consents, it is necessary to forward the Android's onBackPressed system event.

    override fun onBackPressed() {
        if (predefinedUI != null) {
            predefinedUI.onBackButtonPress()
        } else {
            super.onBackPressed()
        }
    }
    

    Parent layout limitations

    We strongly recommend not injecting the view into a complex layout. For example, inside scroll views or a CoordinatorLayout.

Dismissing the CMP

The dismiss block provided will be called once the CMP has collected and is done saving consent. Depending on how you present your CMP, you will want to provide the according dismiss function to update your layout/view.

This completition block will be called after each of the following actions:

  • Accept All
  • Denny All
  • Save Settings
  • Close Button
  1. On your Usercentrics prefab/gameobject inspector, enable the Predefined UI checkbox. Predefined UI Enabled

  2. Present the CMP and provide a dismiss block.

    Usercentrics.Instance.ShowPredefinedUI(() =>
    {
        ...
    });