Info
Content

[iOS] 2. API Documentation

The CMPManager class provides methods to manage user consent for data processing and tracking. This documentation covers the main methods available for mobile app integration.

Initialization

setUrlConfig(:UrlConfig)

Sets the URL configuration for the Consent Manager.

Parameters:

  • config: A UrlConfig object containing the following properties:
    • id: String - The CMP ID
    • domain: String - The domain for consent management
    • language: String - The language code (e.g., "EN")
    • appName: String - The name of your app

Returns: Void

Example:

CMPManager.shared.setUrlConfig(UrlConfig(
    id: "0a000000000a1",
    domain: "delivery.consentmanager.net",
    language: "EN",
    appName: "MyApp"
))

setWebViewConfig(_ :ConsentLayerUIConfig)

Configures the appearance and behavior of the consent WebView. You can set the position where the WKWebiew object displaying the consent layer will appear, like full screen, on the lower half of the screen or its upper half. Also, the background style can be applied, as well as the corner radius, whether it will respect the safe area of the device, as well as whether it will respond to orientation changes or not, in case your mobile works only in one single orientation, which usually happens with games that only use the horizontal configuration of the device's screen.   

Parameters:

  • config: A ConsentLayerUIConfig object with the following properties:
    • position: Position - The position of the WebView (e.g., .fullScreen)
    • backgroundStyle: BackgroundStyle - The background style (e.g., .dimmed)
    • cornerRadius: CGFloat - The corner radius of the WebView
    • respectsSafeArea: Bool - Whether to respect the safe area
    • allowsOrientationChanges: Bool - Whether to allow orientation changes

Returns: Void

Example:

CMPManager.shared.setWebViewConfig(ConsentLayerUIConfig(
    position: .fullScreen,
    backgroundStyle: .dimmed(.black, 0.5),
    cornerRadius: 10,
    respectsSafeArea: true,
    allowsOrientationChanges: true
))

setPresentingViewController(_ :viewController)

Sets the view controller that will present the consent layer. Usually you pass self as the current View Controller. 

Parameters:

  • viewController: UIViewController - The view controller to present the consent layer

Returns: Void

Example:

CMPManager.shared.setPresentingViewController(self)

checkIfConsentIsRequired(:completion)

Parameters:

  • completion: (Bool) -> Void - A closure called with the result, either true or false.

Returns: Voice

Example:

CMPManager.shared.checkIfConsentIsRequired { required in
    print("Consent is required: \(required)")
}

checkWithServerAndOpenIfNecessary(:completion)

Checks with the server if consent is required and opens the consent layer if necessary. This will make a network call to our servers via the WKWebView created inside our SDK, consuming one pageview in the process. This network call will send a message to our backend via JavaScript, whih will detect whether the device has a valid consent or not, which will in turn determine whether the consent layer needs to be displayed or not.  

Parameters:

  • completion: A closure called when the operation completes.

Returns: Void

Example:

CMPManager.shared.checkWithServerAndOpenIfNecessary { error in
    if let error = error {
        print("Error: \(error.localizedDescription)")
    } else {
        print("Check completed successfully")
    }
}

jumpToSettings(:completion)

Parameters:

  • completion: A closure called when the operation completes

Returns: Void

Example:

CMPManager.shared.jumpToSettings { error in
    if let error = error {
        print("Error: \(error.localizedDescription)")
    } else {
        print("Jumped to settings successfully")
    }
}

openConsentLayer(:completion)

Parameters:

  • completion: A closure called when the operation completes, returning either a sucess or an error. 

Returns: Void

Page Views consumed: 1

Example:

CMPManager.shared.openConsentLayer { error in
    if let error = error {
        print("Error: \(error.localizedDescription)")
    } else {
        print("Consent layer opened successfully")
    }
}

exportCMPInfo()

Exports the current consent information stored on the device as a string. This method retrieves the consent string from the UserDefaults area of the device and returns it. Usually this information is passed to the importCMPInfo method.

Returns: String - The exported consent information

Example:

let cmpInfo = CMPManager.shared.exportCMPInfo()
print("Exported CMP info: \(cmpInfo)")

hasPurposeConsent(id:)

Note: Always check for hasUserChoice() first! In cases when no choice exists, the function may return unexpected return values.

Parameters:

  • id: String - The ID of the purpose to check

Returns: Bool - True if consent is given, false otherwise

Example:

let hasPurposeConsent = CMPManager.shared.hasPurposeConsent(id: "c53")
print("Has consent for purpose c53: \(hasPurposeConsent)")

hasUserChoice()

Checks if the user has made a choice regarding consents and this consent is stored on the device. It means that the user either accepted all consents, rejected all of them, or made a mixed choice of rejected and accepted consents, depending on the CMP design, which might allow the users to allow some of the consents and reject others. This information will only be up-to-date after the consent is properly persisted in the UserDefaults area, so if you're checking right after using methods that trigger changes in the consent like openConsentLayer, acceptAll or rejectAll, for example, then wait until the callback from those methods is triggered before accessing the method hasUserChoice, to make sure that the information is up-to-date.

Returns: Bool - true if the user has made a choice, false otherwise

Example:

let hasChoice = CMPManager.shared.hasUserChoice()
print("User has made a choice: \(hasChoice)")

hasVendorConsent(:String)

Parameters:

  • id: String - The ID of the vendor to check

Returns: Bool - True if consent is given, false otherwise

Example:

let hasVendorConsent = CMPManager.shared.hasVendorConsent(id: "s2789")
print("Has consent for vendor s2789: \(hasVendorConsent)")

Purpose and Vendor Management

getAllPurposesIDs()

Retrieves all purpose IDs stored on the device, according to the CMP configurations. This information will only be up-to-date after the consent is properly persisted in the UserDefaults area, so if you're checking right after using methods that trigger changes in the consent like openConsentLayer, acceptAll or rejectAll, for example, then wait until the callback from those methods is triggered before accessing the method hasUserChoice, to make sure that the information is up-to-date.

Returns: [String] - An array of all purpose IDs

Example:

let allPurposes = CMPManager.shared.getAllPurposesIDs()
print("All purposes: \(allPurposes)")

getAllVendorsIDs()

Retrieves all vendor IDs stored on the device, according to the CMP configurations. This information will only be up-to-date after the consent is properly persisted in the UserDefaults area, so if you're checking right after using methods that trigger changes in the consent like openConsentLayer, acceptAll or rejectAll, for example, then wait until the callback from those methods is triggered before accessing the method hasUserChoice, to make sure that the information is up-to-date.

Returns: [String] - An array of all vendor IDs

Example:

let allVendors = CMPManager.shared.getAllVendorsIDs()
print("All vendors: \(allVendors)")

getDisabledPurposesIDs()

Retrieves the IDs of all disabled purposes stored on the device, according to the CMP configurations and the user choices. If the user accepts all the consents, this will be empty.  This information will only be up-to-date after the consent is properly persisted in the UserDefaults area, so if you're checking right after using methods that trigger changes in the consent like openConsentLayer, acceptAll or rejectAll, for example, then wait until the callback from those methods is triggered before accessing the method hasUserChoice, to make sure that the information is up-to-date.

Returns: [String] - An array of disabled purpose IDs

Example:

let disabledPurposes = CMPManager.shared.getDisabledPurposesIDs()
print("Disabled purposes: \(disabledPurposes)")

getDisabledVendorsIDs()

Retrieves the IDs of all disabled vendors stored on the device, according to the CMP configurations. If the user accepts all the consents, this will be empty. This information will only be up-to-date after the consent is properly persisted in the UserDefaults area, so if you're checking right after using methods that trigger changes in the consent like openConsentLayer, acceptAll or rejectAll, for example, then wait until the callback from those methods is triggered before accessing the method hasUserChoice, to make sure that the information is up-to-date.

Returns: [String] - An array of disabled vendor IDs

Example:

let disabledVendors = CMPManager.shared.getDisabledVendorsIDs()
print("Disabled vendors: \(disabledVendors)")

getEnabledPurposesIDs()

Retrieves the IDs of all enabled purposes stored on the device, according to the CMP configurations. If the user rejects all the consents, this will be empty. This information will only be up-to-date after the consent is properly persisted in the UserDefaults area, so if you're checking right after using methods that trigger changes in the consent like openConsentLayer, acceptAll or rejectAll, for example, then wait until the callback from those methods is triggered before accessing the method hasUserChoice, to make sure that the information is up-to-date.

Returns: [String] - An array of enabled purpose IDs

Example:

let enabledPurposes = CMPManager.shared.getEnabledPurposesIDs()
print("Enabled purposes: \(enabledPurposes)")

getEnabledVendorsIDs()

Retrieves the IDs of all enabled vendors stored on the device. If the user rejects all the consents, this will be empty. This information will only be up-to-date after the consent is properly persisted in the UserDefaults area, so if you're checking right after using methods that trigger changes in the consent like openConsentLayer, acceptAll or rejectAll, for example, then wait until the callback from those methods is triggered before accessing the method hasUserChoice, to make sure that the information is up-to-date.

Returns: [String] - An array of enabled vendor IDs

Example:

let enabledVendors = CMPManager.shared.getEnabledVendorsIDs()
print("Enabled vendors: \(enabledVendors)")

acceptAll(:completion)

Parameters:

  • completion: A closure called when the operation completes

Returns: Void

Example:

CMPManager.shared.acceptAll { error in
    if let error = error {
        print("Error: \(error.localizedDescription)")
    } else {
        print("All consents accepted successfully")
    }
}

acceptPurposes(:purposes:updatePurpose:completion)

Parameters:

  • purposes: [String] - An array of purpose IDs to accept
  • updatePurpose: Bool - Whether to update related purposes
  • completion: A closure called when the operation completes

Returns: Void

Example:

CMPManager.shared.acceptPurposes(["c52", "c53"], updatePurpose: true) { error in
    if let error = error {
        print("Error: \(error.localizedDescription)")
    } else {
        print("Purposes accepted successfully")
    }
}

acceptVendors(:vendors:completion)

Parameters:

  • vendors: [String] - An array of vendor IDs to accept
  • completion: A closure called when the operation completes

Returns: Void

Example:

CMPManager.shared.acceptVendors(["s2790", "s2791"]) { error in
    if let error = error {
        print("Error: \(error.localizedDescription)")
    } else {
        print("Vendors accepted successfully")
    }
}

importCMPInfo(:String:completion)

Parameters:

  • cmpString: String - The CMP string to import
  • completion: A closure called when the operation completes

Returns: Void

Example:

let cmpString = "Q1FERkg3QVFERkg3QUFmR01CSVRCQkVnQUFBQUFBQUFBQWlnQUFBQUFBQUEjXzUxXzUyXzUzXzU0XzU1XzU2XyNfczI3ODlfczI3OTBfczI3OTFfczI2OTdfczk3MV9VXyMxLS0tIw"
CMPManager.shared.importCMPInfo(cmpString) { error in
    if let error = error {
        print("Error: \(error.localizedDescription)")
    } else {
        print("CMP info imported successfully")
    }
}

rejectAll(:completion)

Parameters:

  • completion: A closure called when the operation completes

Returns: Void

Example:

CMPManager.shared.rejectAll { error in
    if let error = error {
        print("Error: \(error.localizedDescription)")
    } else {
        print("All consents rejected successfully")
    }
}

rejectPurposes(:purposes:updateVendor:completion)

Parameters:

  • purposes: [String] - An array of purpose IDs to reject
  • updateVendor: Bool - Whether to update related vendors
  • completion: A closure called when the operation completes

Returns: Void

Example:

CMPManager.shared.rejectPurposes(["c52", "c53"], updateVendor: true) { error in
    if let error = error {
        print("Error: \(error.localizedDescription)")
    } else {
        print("Purposes rejected successfully")
    }
}

rejectVendors(:vendors:completion)

Parameters:

  • vendors: [String] - An array of vendor IDs to reject
  • completion: A closure called when the operation completes

Returns: Void

Example:

CMPManager.shared.rejectVendors(["s2790", "s2791"]) { error in
    if let error = error {
        print("Error: \(error.localizedDescription)")
    } else {
        print("Vendors rejected successfully")
    }
}

 

resetConsentManagementData(completion:)

Parameters:

  • completion: A closure called when the operation completes

Returns: Void

Example:

CMPManager.shared.resetConsentManagementData { error in
    if let error = error {
        print("Error: \(error.localizedDescription)")
    } else {
        print("Consent management data reset successfully")
    }
}

App Tracking Transparency (ATT)

requestATTAuthorization(completion:)

Requests App Tracking Transparency authorization from the user. For further information, please check the official Apple documentation

Parameters:

  • completion: (ATTAuthorizationStatus) -> Void - A closure called with the resulting authorization status

Returns: Void

Availability: iOS 14.0+

Example:

if #available(iOS 14, *) {
    CMPManager.shared.requestATTAuthorization { status in
        switch status {
        case .authorized:
            print("ATT: User authorized tracking")
        case .denied:
            print("ATT: User denied tracking")
        case .restricted:
            print("ATT: Tracking is restricted")
        case .notDetermined:
            print("ATT: Status not determined")
        @unknown default:
            print("ATT: Unknown status")
        }
    }
} else {
    print("ATT is not available on this device")
}

getATTAuthorizationStatus()

Gets the current App Tracking Transparency authorization status. For further information, please check the official Apple documentation.

Returns: ATTAuthorizationStatus - The current authorization status

Availability: iOS 14.0+

Example:

if #available(iOS 14, *) {
    let status = CMPManager.shared.getATTAuthorizationStatus()
    print("Current ATT status: \(status)")
} else {
    print("ATT is not available on this device")
}

CMPManagerDelegate events

didReceiveConsent(consent: String, jsonObject: [String: Any])

This is triggered when the consent layer was closed after the user updating his/her consents OR when invoking methods that cause changes in the consents, like acceptAll, rejectAll, acceptVendors, rejectVendors, etc. It means that the user accepted of rejected some of all of the consents, and that those were correctly saved in the device.

didShowConsentLayer

This is triggered when the consent layer was actually displayed. It means that there was not a consent valid in the device, so a new one should be collected.

didCloseConsentLayer


This is triggered when the SDK checked the need for a consent, but it was not needed and the layer was not displayed. It means that there is already a valid in the device, so a new one is not necessary and tue consent layer will not be displayed.

didReceiveError

This is triggered when the SDK faced any error, returning its code.

 

Back to top