Info
Content

[iOS] 1. consentmanager SDK Integration

On this document, you'll find general information on how to integrate our SDK to your project. For further details, please refer to our API Reference documentation. 

1. Installation

consentmanager SDK is a comprehensive solution for managing user consent in mobile applications. Designed to handle GDPR compliance, user privacy preferences, and ad tracking transparency, this SDK provides a seamless integration for iOS and Android platforms. Additionally, it offers wrapper plugins/bridges for React Native, Flutter, and Unity, making it versatile across various development environments.

Steps - High Level Description

  1. Integration and Configuration:

    • Integrate the SDK into your app.
    • Configure the SDK settings according to your needs.
  2. Creating an Instance and displaying the Consent Layer:

    • On app startup, create an instance of the CMPManager class. This instance will handle the consent process.
    • The SDK will automatically display the consent screen if needed.
  3. Processing user's consent data:

    • Once consents are collected, info is stored and is available for querying through different properties and methods exposed by our SDK. You'll have information about rejected or accepted consents, vendors and purposes.

1.1 Integration and Configuration

Option 1: CocoaPods

Add the following line to your Podfile:

pod 'cm-sdk-ios-v3', '3.0.0'

Then run:

pod install --repo-update
Option 2: Swift Package Manager
  1. Download the latest XCFramework from our GitHub releases page.
  2. On XCode, go to the menu File >  Add Package Dependency.
  3. Add the SDK Repository URL above
  4. SPM will now fetch the repository and ask you to select a version.
    You can choose to add the package by selecting a version rule:
    Up to Next Major: This will update the package up to the next major version. It is the recommended option as it adds updates which do not have breaking changes.
    Up to Next Minor: This will update the package up to the next minor version.
    Exact: This will lock the package to a specific version. No updates will be installed.
  5. Import the SDK by inserting in the top of the classes implementing the SDK's methods the line below:
import cm_sdk_ios_v3
  1. In your target's settings, go to "General" > "Frameworks, Libraries, and Embedded Content" and ensure the framework is set to "Embed & Sign".

1.2 Creating an instance and displaying consent layer

Within the app-start (your viewDidLoad function), you must create an instance of class CMPManager. You'll need to set up two objects that will be passed to the getInstance method:  UrlConfig, which handles your CMP configuration, like Code-ID and default language, and ConsentLayerUIConfig. which will configure the looks of the WKWebView that will display the consent layer. After that, you'll pass the current ViewController using the method setPresentingViewController, and attribute the delegate, like shown below. On the example below, you cand find both of the objects being passed. The checkWithServerAndOpenIfNecessary() function will automatically fetch the necessary data from our server and determine if the consent screen needs to be shown or not. If so, the SDK will automatically show the consent screen at this point, via a WKWebView created by our SDK, which will display the consent layer with the text and buttons according to your CMP configurations (chosen via the Code-ID of your CMP), collect the data and persist the consent information in the NSUserDefaults area of the device, so the app can display the targeted ads accordingly. 

Please note that it is vital to declare and initialize the CMPManager SDK in the viewDidLoad method, otherwise the view may not be ready to use and the SDK may fail. Also, please ensure you are using the correct configuration data. The configuration data can be found in your consentmanager account at Menu > CMPs > Get Code for Apps > Code-ID

Also, please note that the functionalities related to determining whether the consent is needed or not, as well as the display of the consent layer, depend on a reliable network connection. If there is no connection available or if the mechanism of retry fails to reach our server, the didReceiveError event will return a timeout error, and so the SDK will be totally unable to determine the need for a consent, as it will be totally unable to display the consent layer. Please ensure your logic takes this into account.

Example:

import cm_sdk_ios_v3

class YourViewController: UIViewController, CMPManagerDelegate {
	override func viewDidLoad() {
	super.viewDidLoad()

	let cmpManager = CMPManager.shared

    cmpManager.setUrlConfig(UrlConfig(
    id: "your_id_here",                   	// example: a000aaaaa1a 
    domain: "your_domain_here",				// usually, delivery.consentmanager.net 
    language: "your_language_here",			// example: DE
    appName: "Your App Name"))				// example: testApp

    cmpManager.setWebViewConfig(ConsentLayerUIConfig(
    position: .fullScreen,
    backgroundStyle: .dimmed(.black, 0.5),
    cornerRadius: 5,
    respectsSafeArea: true,
    allowsOrientationChanges: true))

    cmpManager.setPresentingViewController(self)
    cmpManager.delegate = self

    cmpManager.checkWithServerAndOpenIfNecessary { error in
        if let error = error {
            print("Error initializing consent: \(error)")
        } else {
            print("ConsentManager initialized and consent received and stored on the device's NSUserDefaults.")
        }
    }
}

1.3 Processing users' consent data

Checking users's consents

Our SDK offer different methods to check and retrieve consent information. The main methods are displayed in the example below:

// On the example below retrieved from our Demo App, we have some examples 
// of how to check consents from the user, either accepted or rejected. 

let hasConsent = CMPManager.shared.hasUserChoice()     					// checks if the user has already accepted/rejected consents
let hasPurposeC53 = CMPManager.shared.hasPurposeConsent(id: "c53")		// checks if the user accepted the purpose "c53"
let hasVendorS2790 = CMPManager.shared.hasVendorConsent(id: "s2790")	// checks if the user accepted the vendor "s2790"

For further information about the other methods, please refer to our full API Documentation

Reopening the Consent Layer to check the users' choices

In order to allow the user to verify or change their choices, you can simply call openConsentLayer()

cmpManager.openConsentLayer()

This method will display the consent layer via the same WKWebView instance created in the previous steps. 

In some cases an native app might contain webviews in order to display information, like advertising or content. In order to transmit the consent information from the SDK to the webview, you can retrieve the consent string using:

consentData = cmpManager.exportCMPInfo()

This will export the consent information and all further data that is needed by the CMP. You can then pass this information to the CMP that is in your webview by adding it to the URL that is called in the webview.

If, otherwise, you need to import the consent string using the SDK, you can use the example below:

let consentStringToImport = "Q1FERkg3QVFERkg3QUFmR01CSVRCQkVnQUFBQUFBQUFBQWlnQUFBQUFBQUEjXzUxXzUyXzUzXzU0XzU1XzU2XyNfczI3ODlfczI3OTBfczI3OTFfczI2OTdfczk3MV9VXyMxLS0tIw"
cmpManager.importCMPInfo(consentStringToImport)

Integration with Apple Tracking Transparency (ATT)

In case you are using tracking or analytics in your app, we recommend to read the guide on ATT implementation here.

Creating a custom layout

To create a customized view of the WKWebView, like changing its positioning or background, for example, you can change the configuration passed to the ConsentLayerUIConfig object like below:

ConsentLayerUIConfig(
    position: .halfScreenTop,
    backgroundStyle: .dimmed(.grey, 0.75),
    cornerRadius: 20,
    respectsSafeArea: false,
    allowsOrientationChanges: true)

Logging

When using our iOS SDK, you may find the need to debug or analyze log information for various purposes. The logs generated by our SDK are tagged under "CMP", allowing you to easily filter and view only the relevant logs. For further information, please refer to this section of our documentation.

 

Back to top