Skip to content

Common Functions for Custom UI

Change Language

If you have support for multiple languages, you can use this method to load all content with one of the available languages you have set on the Admin Interface. These can be found in settings.ui.language.available.

guard let settings: UCSettings = usercentrics.getSettings() else { return }
let language: String = settings.ui.language.available.first // Example taking first language available

usercentrics.changeLanguage(language: language, callback: {
    // Content provided in the Data Source methods will now be available in the new language.
    // Update UI
}, onFailure: { error in
    // Handle non-localized error
})
val settings = usercentrics.getSettings()
val language = settings.ui.language.available.first() // Example taking first language available

usercentrics.changeLanguage(language,
    callback = {
        // Content provided in the Data Source methods will now be available in the new language.
        // Update UI
    },
    onFailure = { error ->
        // Handle non-localized error
    }
)

Load a language from the start

As you might have been able to see on the Configure the SDK page, you may provide a default language on the UserOptions object to load the required language from the beginning.

Managing User Sessions

A controllerID is a Usercentrics generated ID, used to identify a user's consent history.

In the moment that a user provides consent, the SDK will generate a new controllerID and store it locally. You can always access this controllerID from your settings object:

guard let settings = usercentrics?.getSettings() else { return }
let controllerID = settings.controllerId
val settings = usercentrics.getSettings()
val controllerID = settings.controllerId

Once you have this ID, you may use it to share consent between your Usercentrics supported platforms, such as iOS, Android, Web, by attaching it to your own authenticated user and restoring their session with one of two ways:

  1. Passing the controllerID in UserOptions, so that the user's consent history and settings get loaded on initialization.
  2. Using restoreUserSession to load your user's consent history and settings, at any point after initialization.
usercentrics.restoreUserSession(controllerId: controllerId) { initialValues in
    // Handle initialValues as needed
}) { error in
    // Handle non-localized error
}}
usercentrics.restoreUserSession(controllerId = controllerId,
    callback = { initialValues ->
        // Handle initialValues as needed
    }, onFailure = { error ->
        // Handle non-localized error
    }
)

Reset a User Session

Additionally, you also have the method resetUserSession that will clean up all local storage, and generate a new ControllerID. This method is handy on logout.

WebView User Session Continuity

For cases where some parts of your app use WebViews, we provide a mechanism to inject a user session, so that the CMP on the WebView is not shown every time the user opens it.

To implement this feature the SDK offers the method getUserSessionData, which returns a String (JSON) with the user session.

This String must then be injected into a global variable called UC_UI_USER_SESSION_DATA in your WKWebView.

let sessionData = usercentrics.getUserSessionData()

let script = """
    window.UC_UI_USER_SESSION_DATA = \(sessionData);
"""
let userScript = WKUserScript(source: script, injectionTime: .atDocumentStart, forMainFrameOnly: true)
let contentController = WKUserContentController()
contentController.addUserScript(userScript)

let preferences = WKPreferences()
preferences.javaScriptEnabled = true

let webConfiguration = WKWebViewConfiguration()
webConfiguration.preferences = preferences
webConfiguration.userContentController = contentController

webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self

let myURL = URL(string:"https://<some_url>")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)

This data must be injected using a JavaScriptInterface with a method getSessionData.

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

webView.addJavascriptInterface(SampleJavascriptInterface(usercentrics), "ucMobileSdk")
webView.loadUrl("https://<some_url>")
class SampleJavascriptInterface(private val usercentrics: Usercentrics) {

    @JavascriptInterface
    fun getUserSessionData(): String? {
        return usercentrics.getUserSessionData()
    }
}

Compatibility

User Session injection is only supported if your WebView is running version 1.4.0 or higher of the Usercentrics BrowserUI.