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 = spConsentEngine?.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.

spConsentEngine.clearSession()