Info
Content

[iOS] 3. API Documentation

acceptAll

Parameters
Name Type Description
onFinish function Callback for handling consent received event. it will be called when consent is received and processed.
Return

No return value.

Example
func onFinish() {
    print("Notification: New consent was received")
}

// Accept all consent with a callback
cmpConsentTool.acceptAll(onFinish)

check

Parameters
Name Type Description
onCmpLayerOpen function Callback when the consent layer needs to be open.
isCached Boolean When TRUE, the request will be cached and the response will be saved
Return

No return value.

Example
@objc func checkCmpLayer() {
    print("Checking need for consent.")

    cmpManager?.check(
        {
            // Callback for when the consent layer needs to be opened
            print("Consent layer will open")
            self.cmpConsentTool?.openView()
        }, 
        isCached: true
    )
}

checkAndOpenConsentLayer

Checks if user's consent is required and, if so, opens the consent layer UI. This method consumes one pageview. 

Parameters
None.
Return

No return value.

Example
@objc func checkAndOpenConsentLayerAction() {
    cmpManager?.checkAndOpenConsentLayer()
}

consentRequestedToday

Checks if the consent layer was requested today. This method consumes no pageviews. 

Parameters
None.
Return

TRUE if the user's consent was already requested today.

FALSE otherwise.

Example
@objc func consentRequestedTodayAction() {
    if let calledToday = cmpManager?.consentRequestedToday() {
        print("Consent requested today: \(calledToday)")
    } else {
        print("Failed to retrieve consent request status for today.")
    }
}

disablePurposeList

Disables a list of purposes and updates the given consent. This method consumes one pageview. 

Parameters
Name Type Description
purposes Array List of purpose ids to disable.
onFinish function (Optional) Callback for handling consent received event. it will be called when consent is received and processed.
Return

No return value.

Example
func onFinish() {
    print("Notification: New consent received")
}

let purposes = ["1", "2"]

// Option with callback
cmpManager?.disablePurposeList(purposes, onFinish: onFinish)

// Option without callback
cmpManager?.disablePurposeList(purposes)

disableVendorList

Disables a list of vendors and updates the given consent. This method consumes one pageview. 

Parameters
Name Type Description
vendors Array List of vendor ids to disable.
onFinish function (Optional) Callback for handling consent received event. It will be called when consent is received and processed.
Return

No return value.

Example
func onFinish() {
    print("Notification: New consent received")
}

let vendors = ["1", "2"]

// Option with callback
cmpManager?.disableVendorList(vendors, onFinish: onFinish)

// Option without callback
cmpManager?.disableVendorList(vendors)

enablePurposeList

Enables a list of purposes and updates the given consent. This method consumes one pageview. 

Parameters
Name Type Description
purposes Array List of purpose ids to enable.
onFinish function (Optional) Callback for handling consent received event. It will be called when consent is received and processed.
Return

No return value

Example
func onFinish() {
    print("Notification: New consent received")
}

let purposes = ["1", "2"]

// Option with callback
cmpManager?.enablePurposeList(purposes, onFinish: onFinish)

// Option without callback
cmpManager?.enablePurposeList(purposes)

enableVendorList

Enables a list of vendors and updates the given consent. This method consumes one pageview. 

Parameters
Name Type Description
vendors Array List of vendor ids to enable.
onFinish function (Optional) Callback for handling consent received event. iI will be called when consent is received and processed.
Return

No return value.

Example
func onFinish() {
    print("Notification: New consent received")
}

let vendors = ["1", "2"]

// Option with callback
cmpManager?.enableVendorList(vendors, onFinish: onFinish)

// Option without callback
cmpManager?.enableVendorList(vendors)

exportCmpString

Exports the current CMP string representing the user's consent preferences. This method is accessed in a static way. This method consumes no pageviews. 

Parameters
None.
Return

The consent string.

Example
@objc func exportConsentStringAction() {
    let exportString = cmpManager.exportCmpString()
    print(exportString ?? "No string to export.");
}

getAllPurposes

Returns all purposes' IDs. This method consumes no pageviews. 

Parameters
None.
Return

A comma-separated string containing all purpose IDs.

Example
@objc func getAllPurposesAction() {
    if let allPurposes = cmpManager?.getAllPurposes() {
        print("All purposes: \(allPurposes)")
        // Additional actions for getAllPurposes function can be added here
    } else {
        print("Failed to retrieve all purposes.")
    }
}

getAllPurposeList

Returns a list of all purpose IDs for a given CMP. This method consumes no pageviews. 

Parameters
None.
Return

A List<String> containing all purpose IDs.

Example
@objc func getAllPurposeListAction() {
    // Action for getAllPurposeList function
    if let allPurposes = cmpManager?.getAllPurposesList() {
        print("All purposes list: \(allPurposes.joined(separator: ", "))")
    } else {
        print("Failed to retrieve all purposes list.")
    }
}

getAllVendors

Returns all vendors' IDs for a given CMP. This method consumes no pageviews. 

Parameters
None.
Return

A comma-separated string containing all vendors IDs.

Example
@objc func getAllVendorsAction() {
    if let allPurposes = cmpManager?.getAllVendors() {
        print("Printing all Vendors: \(allPurposes)")
    } else {
        print("Failed to retrieve all vendors.")
    }
}

getAllVendorsList

Returns a list of all vendors' IDs for a given CMP. This method consumes no pageviews. 

Parameters
None.
Return

A List<String> containing all vendor IDs.

Example
@objc func getAllVendorsListAction() {
    if let allVendors = cmpManager?.getAllVendorsList() {
        print("All vendors list: \(allVendors.joined(separator: ", "))")
    } else {
        print("Failed to retrieve all vendors list.")
    }
}

getConsentString

Returns a string with the last given consent. This method consumes no pageviews. 

Parameters
None.
Return

A string with the last consent acknowledged by the user.

Example
@objc func getAllVendorsListAction() {
    if let allVendors = cmpManager?.getAllVendorsList() {
        print("All vendors list: \(allVendors.joined(separator: ", "))")
    } else {
        print("Failed to retrieve all vendors list.")
    }
}

getDisabledPurposes

Returns all disabled purposes' IDs for a given CMP. This method consumes no pageviews. 

Parameters
None.
Return

A comma-separated string containing all disabled purposes' IDs.

Example
@objc func getDisabledPurposesAction() {
    if let disabledPurposes = cmpManager?.getDisabledPurposes() {
        print("Disabled purposes: \(disabledPurposes)")
    } else {
        print("Failed to retrieve disabled purposes.")
    }
}

getDisabledVendors

Returns all disabled vendors' IDs for a given CMP. This method consumes no pageviews. 

Parameters
None.
Return

A comma-separated string containing all disabled vendors' IDs.

Example
@objc func getDisabledVendorsAction() {
    if let disabledVendors = cmpManager?.getDisabledVendors() {
        print("Disabled vendors: \(disabledVendors)")
    } else {
        print("Failed to retrieve disabled vendors.")
    }
}

getEnabledPurposes

Returns all enabled vendors' IDs for a given CMP. This method consumes no pageviews. 

Parameters
None.
Return

A comma-separated string containing all enabled vendors' IDs.

Example
@objc func getEnabledVendorsAction() {
    if let enabledVendors = cmpManager?.getEnabledVendors() {
        print("Enabled vendors: \(enabledVendors)")
    } else {
        print("Failed to retrieve enabled vendors.")
    }
}

getEnabledPurposeList

Returns a list all enabled purposes' IDs for a given CMP. This method consumes no pageviews. 

Parameters
None.
Return

A List<String> containing all enabled purposes' IDs.

Example
@objc func getEnabledPurposesAction() {
    if let enabledPurposes = cmpManager?.getEnabledPurposes() {
        print("Enabled purposes: \(enabledPurposes.joined(separator: ", "))")
    } else {
        print("Failed to retrieve enabled purposes list.")
    }
}

getEnabledVendors

Returns all enabled vendors' IDs for a given CMP. This method consumes no pageviews. 

Parameters
None.
Return

A comma-separated string containing all enabled vendors' IDs.

Example
@objc func getEnabledVendorsAction() {
    if let enabledVendors = cmpManager?.getEnabledVendors() {
        print("Enabled vendors: \(enabledVendors)")
    } else {
        print("Failed to retrieve enabled vendors.")
    }
}

getEnabledVendorList

Returns a list with all enabled vendors' IDs for a given CMP. This method consumes no pageviews. 

Parameters
None.
Return

A List<String> containing all enabled vendors' IDs.

Example
@objc func getEnabledVendorsAction() {
    if let enabledVendors = cmpManager?.getEnabledVendors() {
        print("Enabled vendors: \(enabledVendors.joined(separator: ", "))")
    } else {
        print("Failed to retrieve enabled vendors.")
    }
}

getGoogleACString

Retrieves the Google additional consent ID (`addtlConsent`) string that was set by the consent manager.

For further information, check the Google Additional Consent technical specification.  

This method consumes no pageviews. 

Parameters
None.
Return

A List<String> containing all enabled vendors' IDs.

Example
@objc func getGoogleConsentStringAction() {
    if let googleACString = cmpManager?.getGoogleACString() {
        print("Google Additional Consent String: \(googleACString)")
    } else {
        print("Failed to retrieve Google Additional Consent String.")
    }
}

getUSPrivacyString

Retrieves the US Privacy String. This U.S. privacy string is a cookie that stores information about disclosures made and choices selected by the website visitor regarding their consumer rights. This method consumes no pageviews. 

Parameters
None.
Return

A String with the US Privacy data set by consentmanager.

Example
@objc func getUSPrivacyStringAction() {
    if let theUSPrivacyString = cmpManager?.getUSPrivacyString() {
        print("US Privacy string contents: \(theUSPrivacyString)")
    } else {
        print("Failed to retrieve US Privacy string.")
    }
}

hasConsent

Checks if the user already provided his consent. The consent layer can have the option to just close the layer. In that case, the user did not provided a consent. This method consumes no pageviews. 

Parameters
None.
Return

A String with the US Privacy data set by consentmanager.

Example
@objc func getHasConsentAction() {
    if let hasConsent = cmpManager?.hasConsent() {
        let consentStatus = hasConsent ? "Yes" : "No"
        print("Consent provided by the user? \(consentStatus)")
    } else {
        print("Failed to retrieve user consent.")
    }
}

hasPurpose (from version 2.5.3 on)

hasPurposeConsent (deprecated)

Checks if the purpose ID is enabled based on the user consent. This method consumes no pageviews. 

Parameters
Name Type Description
purposeId String purpose id
defaultReturn Boolean Value that is returned by default by the method, in case there is no consent yet. See warning below.
Return

TRUE if the user has given consent to the specified purpose.

FALSE otherwise.

Note that if, for any reason related to poor or no internet connection on the device or in the case of absence of a consent, the value that was set via the defaultReturn parameter will be returned.

The defaultReturn parameter do not exist in versions older than 2.5.3.

Example
@objc func getHasPurposeConsentAction() {
    if let hasPurposeConsent = cmpManager?.hasPurpose("purposeID", false) {
        let consentStatus = hasPurposeConsent ? "Yes" : "No"
        print("Purpose has consent provided by the user? \(consentStatus)")
    } else {
        print("Failed to retrieve user purpose consent.")
    }
}

hasVendor (from version 2.5.3 on)

hasVendorConsent (deprecated)

Checks if the vendor ID is enabled based on the user consent. This method consumes no pageviews. 

Parameters
Name Type Description
vendorId String vendor id
defaultReturn Boolean Value that is returned by default by the method, in case there is no consent yet. See warning below.
Return

FALSE otherwise.

Note that if, for any reason related to poor or no internet connection on the device or in the case of absence of a consent, the value that was set via the defaultReturn parameter will be returned.

The defaultReturn parameter do not exist in versions older than 2.5.3.

    Example
    @objc func getHasVendorConsentAction() {
        if let hasVendorConsent = cmpManager?.hasVendor("vendorID") {
            let consentStatus = hasVendorConsent ? "Yes" : "No"
            print("Vendor has consent provided by the user? \(consentStatus)")
        } else {
            print("Failed to retrieve user vendor consent.")
        }
    }
    

    importCmpString

    Imports a CMP string representing the user's consent preferences. The string must be informed AS-IS when retrieved via the exportCmpString() method. This method consumes no pageviews.

    Parameters
    Name Type Description
    consentString  String String representing the consent
    completionHandler function (optional) Callback completion handler to the method.
    Return

    None.

    Example
    let cmpString = "YourConsentStringHere"
    
    // Define the completion handler
    let completionHandler: CmpImportCompletionHandler = { error in
        if let error = error {
            print("Failed to import CMP string: \(error.localizedDescription)")
        } else {
            print("CMP string imported successfully")
        }
    }
    
    cmpManager.importCmpString(cmpString, completion: completionHandler)
    

    initialize

    Initialize function will initialize the consent layer and checks automatically if the user needs a consent or not. This function will eventually open the consent layer and Will interact wit the Apple Tracking Transparency API to ask the user for Consent. This method consumes one pageview.

    Parameters
    Name Type Description
    cmpConfig CmpConfig The CmpConfig object is passed with the info regarding the appId, Domain, appName and language (optional)
    Return

    The CMPManagerobject.

    Example
    // Example of a default implementation of the SDK.
    
    class ViewController: UIViewController {
    
      // Usual implementation of a View Controller
      
      var cmpManager: CMPConsentTool? = nil
    
      override func viewDidLoad() {
    
          super.viewDidLoad()
    
          // Configure your CMP
          let cmpConfig: CmpConfig = CmpConfig.shared.setup(
                                              withId: "<YOUR-CONSENTMANAGER-APP-ID>, example: a000aaaa0a", 
                                              domain: "<YOUR-CONSENTMANAGER-APP-DOMAIN>, example: delivery.consentmanager.net", 
                                              appName: "<YOUR-CONSENTMANAGER-APP-NAME>, example: testApp", 
                                              language: "<YOUR-CONSENTMANAGER-APP-LANGUAGE, example: DE");
    
          // You can also determine log levels or ask for Apple's App Tracking Transparency, for example
          cmpConfig.logLevel = CmpLogLevel.verbose;
          cmpConfig.isAutomaticATTRequest = true;
    
          // Then you pass the cmpConfig to set up and initialize the instance of our SDK		
          cmpManager = CMPConsentTool(cmpConfig: cmpConfig)
                  .withErrorListener(onCMPError)
                  .withCloseListener(onClose)
                  .withOpenListener(onOpen)
                  .withOnCMPNotOpenedListener(onCMPNotOpened)
                  .withOnCmpButtonClickedCallback(onButtonClickedEvent)
                  .initialize()  // This method will trigger the webview loading to collect consent, if necessary
      }
    }

    isConsentRequired

    Parameters
    None.
    Return

    TRUE if the user needs to give a consent.

    FALSE otherwise.

    Example
    @objc func checkIfConsentIsRequired() {
        if let isConsentRequired = cmpConsentTool?.isConsentRequired() {
            let consentRequiredStatus = isConsentRequired ? "Yes" : "No"
            print("Is user consent required? \(consentRequiredStatus)")
        } else {
            print("Failed to determine if user consent is required.")
        }
    }
    

    openView

    Forcefully opens the CMP consent layer webview. This method consumes one pageview.

    Parameters
    None.
    Return

    No return value.

    Example
    self.cmpConsentTool?.openView()

    rejectAll

    Parameters
    Name Type Description
    onFinish function Optional. Callback for handling consent received event. it will be called when consent is received and processed.
    Return

    No return value

    Example
    func onFinish() {
        print("Notification: New consent has been received with a Reject All response.")
    }
    
    // Reject all consent with a callback
    cmpManager.rejectAll(onFinish)
    

    reset

    Resets all properties to default values. This method consumes no pageviews.

    Parameters
    None.
    Return

    No return value.

    Example
    CMPManager.reset()

    withCloseListener

    Adds a close listener to receive notifications when the consent layer is being closed.

    Parameters
    Name Type Description
    closeListener function Callback which will be called when the consent layer is closed.
    Return

    CMPConsentTool

    Example
    func onClose() {
        print("APP: CLOSED")
        // Add custom business logic here
    }
    
    // Initialize cmpManager with configuration and close listener
    cmpManager = CMPConsentTool(cmpConfig: cmpConfig, viewController: self)
        .withCloseListener(onClose)
    

    withOpenListener

    Adds an open listener to receive notifications when the consent layer is being opened.

    Parameters
    Name Type Description
    openListener function Callback which will be called when the consent layer is opened
    Return

    CMPConsentTool

    Example
    func onOpen() {
        print("APP: OPENED")
        // Add custom business logic here
    }
    
    // Initialize cmpManager with configuration and open listener
    cmpManager = CMPConsentTool(cmpConfig: cmpConfig, viewController: self)
        .withCloseListener(onOpen)
    

    withErrorListener

    Adds an error listener to receive notifications when an error occurred.

    Parameters
    Name Type Description
    errorListener function Callback to add action if an error occurred.
    Return

    CMPConsentTool

    Example
    func onCMPError(type: CmpErrorType, message: String?) {
        if let errorMessage = message {
            print(errorMessage)
        } else {
            print("An error occurred.")
        }
    
        switch type {
        case .networkError:
            print("Network error")
        case .timeoutError:
            print("Timeout error")
        case .consentDataReadWriteError:
            print("Consent data read/write error")
        case .unknownError:
            print("Unknown error")
        @unknown default:
            print("Unexpected error")
        }
    }
    
    // Initialize cmpManager with configuration and error listener
    cmpManager = CMPConsentTool(cmpConfig: cmpConfig, viewController: self)
        .withErrorListener(onCMPError)
    

    withOnCMPNotOpenedListener

    Adds a Cmp not opened callback to get notification when consent layer is not opening. Most common reason is that the user already provided a consent.

    Parameters
    Name Type Description
    openListener function Callback which will be called when the consent layer is not opened. 
    Return

    CMPConsentTool

    Example
    func onCMPNotOpened() {
        print("CMP: NOT OPENED")
        // Add custom business logic here
    }
    
    // Initialize cmpConsentTool with configuration and CMP not opened listener
    cmpManager = CMPConsentTool(cmpConfig: cmpConfig, viewController: self)
        .withOnCMPNotOpenedListener(onCMPNotOpened)
    

    withOnCmpButtonClickedCallback

    Sets a callback to receive notifications when the user clicks a button to close the consent layer.

    Parameters
    Name Type Description
    openListener function Callback that will be invoked with the CmpButtonEvent indicating which button was pressed.
    Return

    CMPConsentTool

    Example
    func onButtonClickedEvent(event: CmpButtonEvent) {
        switch event {
        case .acceptAll:
            print("User accepted all.")
        case .rejectAll:
            print("User rejected all.")
        case .save:
            print("User saved custom settings.")
        case .close:
            print("User closed consent layer without giving consent.")
        @unknown default:
            print("Unknown button event.")
        }
    }
    
    // Initialize cmpManager with configuration and button click callback
    cmpManager = CMPConsentTool(cmpConfig: cmpConfig, viewController: self)
        .withOnCmpButtonClickedCallback(onButtonClickedEvent)
    

    withCmpViewControllerConfigurationBlock

    Customise the consentlayer UIViewController.

    Parameters
    Name Type Description

    configurationBlock

    CmpViewControllerConfigurationBlock

    Customise the consentlayer

    UIViewController.

    Return

    CMPConsentTool

    Example
    cmpConsentTool = CMPConsentTool(cmpConfig: cmpConfig)
        .withCmpViewControllerConfigurationBlock { viewController in
            viewController?.modalPresentationStyle = .formSheet
        }
    

    withCmpViewConfigurationBlock

    Customise the UiView of the consentlayer.

    Parameters
    Name Type Description

    configurationBlock

    CmpUIViewConfigurationBlock

    Customise the consentlayer

    UiView

    Return

    CMPConsentTool

    Example
    let cmpLayout = CmpLayout.default()
    cmpLayout?.cornerRadius = 10.0
    cmpLayout?.customLayout = CGRect(x: 0, y: 0, width: 200, height: 300)
    
    cmpConsentTool = CMPConsentTool(cmpConfig: cmpConfig)
        .withCmpViewConfigurationBlock { uiView in
            cmpLayout?.apply(to: uiView)
        }
    

    withUpdateGoogleConsent

    Retrieves Google Consent Mode status. 

    Parameters
    Name Type Description

    consentMap

    [String: String]?

    Consent status
    Return

    CMPConsentTool

    Example
    cmpConsentTool = CMPConsentTool(cmpConfig: cmpConfig)
        .withUpdateGoogleConsent(onCmpUpdateGoogleConsent)
    
    func onCmpUpdateGoogleConsent(consentMap: [String: String]?) {
        guard let consentMap = consentMap else { return }
    
        var firebaseConsentMap: [ConsentType: ConsentStatus] = [:]
    
        consentMap.forEach { key, value in
            if let consentType = convertToFirebaseConsentType(from: key),
               let consentStatus = convertToFirebaseConsentStatus(from: value) {
                firebaseConsentMap[consentType] = consentStatus
            }
        }
    
        Analytics.setConsent(firebaseConsentMap)
    }
    
    func convertToFirebaseConsentType(from key: String) -> ConsentType? {
        switch key {
        case "analytics_storage":
            return .analyticsStorage
        case "ad_storage":
            return .adStorage
        case "ad_user_data":
            return .adUserData
        case "ad_personalization":
            return .adPersonalization
        default:
            return nil
        }
    }
    
    func convertToFirebaseConsentStatus(from value: String) -> ConsentStatus? {
        switch value {
        case "granted":
            return .granted
        case "denied":
            return .denied
        default:
            return nil
        }
    }
    

     

    Back to top