[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
: AUrlConfig
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
: AConsentLayerUIConfig
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)
Consent Layer Management
checkIfConsentIsRequired(:completion)
Checks if consent is required for the user. This will make a network call to our servers via the WKWebView created inside our SDK, which will send a message to our backend via JavaScript. Our backend will detect whether the device has a valid consent or not, and message will be sent back to the WKWebView, determining if it needs to be displayed or not, consuming one pageview in the process. Depending on the message returned, the SDK will interpret it and return its results via the completion handler.
Parameters:
-
completion
: (Bool) -> Void - A closure called with the result, eithertrue
orfalse
.
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)
Opens the consent layer and jumps directly to the settings page. This will make a network call to our servers, consuming one pageview in the process, invoking the display of the WKWebView, but displaying the Settings page instead of the initial page of the consent layer, that gives the users the choices of Accepting All or Rejecting All of the consents. This, instead, will lead to the page where a more granular control over the consents is given to the users, allowing them to fine tune their choices.
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)
Opens the consent layer. It makes a network call to our backend, consuming one pageview in the process. It will display the consent layer allowing the user to either Accept All or Reject All of the choices, or, depending on the CMP design, allowing them to control the consents in a more granular way.
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")
}
}
Consent Status
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:)
Checks if consent has been given for a specific purpose and this consent is stored on the device. It checks the UserDefaults area for the consents accepted or rejected, and filter the ID passed as a parameter, returning true if the consent was accepted or false otherwise.
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)
Checks if consent has been given for a specific vendor and this consent is 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.
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)")
Consent Modification
acceptAll(:completion)
Accepts consent for all purposes and vendors, consuming one pageview in the process. It makes a network call to our backend via a message injected on the WKWebView, which will trigger the acceptance of all the consents, according to the CMP configuration. This information will only be available to the other methods after the callback returns a success or failure, which means it was succesfully processed by our backend and persisted on the device.
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)
Accepts consent for specified purposes, consuming one pageview in the process. It makes a network call to our backend via a message injected on the WKWebView, which will trigger the acceptance of the determined purposes passed as a parameter, according to the CMP configuration. This information will only be available to the other methods after the callback returns a success or failure, which means it was succesfully processed by our backend and persisted on the device.
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)
Accepts consent for specified vendors, consuming one pageview in the process. It makes a network call to our backend via a message injected on the WKWebView, which will trigger the acceptance of the determined vendors passed as a parameter, according to the CMP configuration. This information will only be available to the other methods after the callback returns a success or failure, which means it was succesfully processed by our backend and persisted on the device.
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)
Imports consent information from a CMP string. This will receive a string containing the consent data, usually obtained through the exportCMPInfo
method. This information is persisted in the UserDefaults area of the device, and at the same time is sent to our backend via a message injected in the WKWebView, consuming one pageview in the process.
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)
Rejects consent for all purposes and vendors, consuming one pageview in the process. It makes a network call to our backend via a message injected on the WKWebView, which will trigger the rejection of all the consents, according to the CMP configuration. This information will only be available to the other methods after the callback returns a success or failure, which means it was succesfully processed by our backend and persisted on the device.
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)
Rejects consent for specified purposes, consuming one pageview in the process. It makes a network call to our backend via a message injected on the WKWebView, which will trigger the rejection of the determined purposes passed as a parameter, according to the CMP configuration. This information will only be available to the other methods after the callback returns a success or failure, which means it was succesfully processed by our backend and persisted on the device.
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)
Rejects consent for specified vendors, consuming one pageview in the process. It makes a network call to our backend via a message injected on the WKWebView, which will trigger the rejection of the determined vendors passed as a parameter, according to the CMP configuration. This information will only be available to the other methods after the callback returns a success or failure, which means it was succesfully processed by our backend and persisted on the device.
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:)
Resets all consent management data. This completely erases al the UserDefaults area entries related to consents accepted or rejected by the user. It is similar to completely removing the app from the device.
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.
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.