Implement consent
Learn how to implement consent in a mobile app.
The Adobe Experience Platform Consent mobile extension enables consent preferences collection from your mobile app when using the Adobe Experience Platform Mobile SDK and the Edge Network extension. Learn more about the Consent extension in the documentation.
Prerequisites
- Successfully built and run app with SDKs installed and configured.
Learning objectives
In this lesson, you will:
- Prompt the user for consent.
- Update the extension based on the user response.
- Learn how to get the current consent state.
Ask for consent
If you followed the tutorial from the beginning, you might remember that you have set the default consent in the Consent extension to Pending - Queue events that occur before the user provides consent preferences.
To begin collecting data, you must get consent from the user. In a real-world app, you would want to consult consent best practices for your region. In this tutorial, you get consent from the user by simply asking for it with an alert:
-
You only want to ask the user once for consent. You can do this by combining the Mobile SDK consent with the required authorization for tracking using Apple’s App Tracking Transparency framework. In this app, you assume that when the user authorizes tracking they consent to collecting events.
-
Navigate to Luma > Luma > Utils > MobileSDK in the Xcode Project navigator.
Add this code to the
updateConsent
function.code language-swift // Update consent let collectConsent = ["collect": ["val": value]] let currentConsents = ["consents": collectConsent] Consent.update(with: currentConsents) MobileCore.updateConfigurationWith(configDict: currentConsents)
-
Navigate to Luma > Luma > Views > General > DisclaimerView in Xcode’s Project navigator, which is the view that is shown after installing or reinstalling the application and starting the app for the first time. The user is prompted to authorize tracking per Apple’s App Tracking Transparency framework. If the user authorizes, you also update the consent.
Add the following code to the
ATTrackingManager.requestTrackingAuthorization { status in
closure.code language-swift // Add consent based on authorization if status == .authorized { // Set consent to yes MobileSDK.shared.updateConsent(value: "y") } else { // Set consent to yes MobileSDK.shared.updateConsent(value: "n") }
Get current consent state
The Consent mobile extension automatically suppresses / pends / allows tracking based on the current consent value. You can also access the current consent state yourself:
-
Navigate to Luma > Luma > Utils > MobileSDK in Xcode’s Project navigator.
Add the following code to the
getConsents
function:code language-swift // Get consents Consent.getConsents { consents, error in guard error == nil, let consents = consents else { return } guard let jsonData = try? JSONSerialization.data(withJSONObject: consents, options: .prettyPrinted) else { return } guard let jsonStr = String(data: jsonData, encoding: .utf8) else { return } Logger.aepMobileSDK.info("Consent getConsents: \(jsonStr)") }
-
Navigate to Luma > Luma > Views > General > HomeView in Xcode’s Project navigator.
Add the following code to the
.task
modifier:code language-swift // Ask status of consents MobileSDK.shared.getConsents()
In the above example, you are simply logging the consent status to the console in Xcode. In a real-world scenario, you might use it to modify what menus or options are shown to the user.
Validate with Assurance
- Delete the application from your device or simulator to properly reset and initialize the tracking and consent.
- To connect your simulator or device to Assurance, review the setup instructions section.
- When moving in the app from Home screen to Products screen and back to Home screen, you should see a Get Consents Response event in the Assurance UI.
Next: Collect lifecycle data