Managing User Sessions¶
Restore a User Session (Cross-Device Consent Sharing)¶
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
var settings = Usercentrics.Instance.GetSettings();
var 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:
- Passing the
controllerID
inUserOptions
, so that the user's consent history and settings get loaded on initialization. - 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
}
)
Usercentrics.Instance.RestoreUserSession(controllerId,
(showCMP) =>
{
// Handle showCMP as needed
},
(errorMessage) =>
{
// 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.