Presenting the CMP¶
- On your
UserOptions
object, enable predefinedUI.let userOptions = UserOptions(..., predefinedUI: true, ...)
-
Request an optional
UIViewController
orUINavigationController
, by callinggetPredefinedUI()
from an initialized instance of theUsercentrics
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) { ... }
-
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:
- On your
UserOptions
object, enable predefinedUI.val userOptions = UserOptions(..., predefinedUI = true, ...)
- Register
UsercentricsActivity
in your fragment or activity where you want to use it. Save theActivityResultLauncher
in a member variable in order to use it later.private val usercentricsActivityLauncher = registerForActivityResult(UsercentricsActivityContract()) { services -> // ... }
-
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 theonActivityResult
. You can still useUsercentricsActivityContract
for that purpose:val intent = UsercentricsActivityContract().createIntent(context, arguments) startActivityForResult(intent, requestCode)
In order to use it, the following steps are needed:
-
On your
UserOptions
object, enable predefinedUI.val userOptions = UserOptions(..., predefinedUI = true, ...)
-
Request a nullable
View
, by callinggetPredefinedUI()
from an initialized instance of theUsercentrics
class and provide a dismiss block. Thelayout
object is theViewGroup
where you want to add theView
.var predefinedUI: View? = null predefinedUI = usercentrics.getPredefinedUI(viewContext = myActivity) { layout.removeView(this.predefinedUI) predefinedUI = null ... }
-
Present the View
layout.addView(predefinedUI)
-
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
-
On your Usercentrics prefab/gameobject inspector, enable the Predefined UI checkbox.
-
Present the CMP and provide a dismiss block.
Usercentrics.Instance.ShowPredefinedUI(() => { ... });