Skip to content

Advanced Usage

You can listen to consent events by SPConsentEventDelegate:

class MobileConsentEventDelegate: SPConsentEventDelegate{
var applicationId: String = "YOUR_APPLICATION_ID"
func onConsentAction(data: SPMobileConsent.SPDataMessage<SPMobileConsent.SPConsentEvent>) {
//Custom logic
}
}

Add a delegate to listen to consent events

spConsentEngine.addDelegate(forCode: MOBILE_CONSENT_EVENT_CODE, delegate: eventDelegate)

Remove delegate

spConsentEngine.removeDelegate(forCode: MOBILE_CONSENT_EVENT_CODE)

Here, MOBILE_CONSENT_EVENT_CODE is a unique code you can use to add/remove delegate and track updates on consent actions.

Additional Way to Listen to Consent Events: As an additional way to listen to consent events, you can now observe consent events using the following method:

spConsentEngine.addObserver(code: YOUR_UNIQUE_EVENT_CODE, observer: self, callback: {
event in
//Custom logic
})
spConsentEngine.removeObserver(forCode: YOUR_UNIQUE_EVENT_CODE)

Event Codes

When using the SDK to listen to consent events, it’s important to understand how event codes work.

Event codes are unique identifiers that is used to register/unregister consent event delegates and observers. The event code helps distinguish between different consent events, particularly when dealing with multiple delegates, observers or consent events across different ViewControllers or classes.

To enable detailed logs for debugging during development, activate the logging flags provided by the SDK:

SPLogger.ERROR_LOGS_ENABLED = true
SPLogger.INFO_LOGS_ENABLED = true
SPLogger.WARNING_LOGS_ENABLED = true

These settings will enable:

  • Error Logs – to capture critical issues.
  • Info Logs – for general SDK activity and flow.
  • Warning Logs – to monitor potential misconfigurations or unexpected behavior.

⚠️ Tip: Disable logging in production to avoid exposing sensitive data.

Each time the app is installed (fresh install) or during a new session, Secure Privacy Mobile Consent SDK generates a unique clientId for the user. This clientId can be retrieved by calling the following method.

let clientId = spConsentEngine?.getClientId(applicationId: "YOUR_APPLICATION_ID")

This clientId can be used to associate the user’s consent with internal identifiers within your app or backend systems. You can also use this clientId for custom user identification and to ensure accurate tracking of consent preferences.

You can get the country code detected by the SDK using:

val countryCode = SPConsentEngineFactory.getLocale(applicationId: "YOUR_APPLICATION_ID")

It returns a country code like US-CA or IN based on the user’s region.

To clear the session, call clearSession(). This method is useful when you want to ensure that all local session-specific sdk data is cleared. It is typically used when a user logs out or resets their preferences.

SPConsentEngineFactory.clearSession()

SPConsentEngineFactory.initialise(...) creates a session-bound context and returns an SPConsentEngine instance for that session. Objects and values obtained through that engine should be treated as valid only while the session remains active.

Calling SPConsentEngineFactory.clearSession():

  • Invalidates the current SDK session
  • Invalidates previously obtained SPConsentEngine instances and session-bound data

⚠️ Warning: After clearing/resetting the session, you must reinitialize the SDK and obtain fresh instances before making any further SDK calls. Reusing old instances can lead to undefined or broken behavior.

Incorrect usage

  • Initialize SDK
  • Store spConsentEngine
  • Call SPConsentEngineFactory.clearSession()
  • Continue using old spConsentEngine
let result = await SPConsentEngineFactory.initialise(key: key)
let spConsentEngine = result.data
SPConsentEngineFactory.clearSession()
// Do not reuse a pre-clear session instance
_ = spConsentEngine?.getConsentStatus(applicationId: "YOUR_APPLICATION_ID")

Correct usage

  • Initialize SDK
  • Use spConsentEngine
  • Call SPConsentEngineFactory.clearSession()
  • Reinitialize SDK
  • Obtain and use a fresh spConsentEngine
var initResult = await SPConsentEngineFactory.initialise(key: key)
var spConsentEngine = initResult.data
// ... use SDK
SPConsentEngineFactory.clearSession()
initResult = await SPConsentEngineFactory.initialise(key: key)
spConsentEngine = initResult.data
_ = spConsentEngine?.getConsentStatus(applicationId: "YOUR_APPLICATION_ID")