PubConsent

iOS Integration

24min

The PubConsent SDK is the integration of our PubConsent CMP into iOS applications.

This integration doesn't require many updates since every change made inside the configurator will be reflected automatically. We support iOS versions from 9 up to the latest one.

Whenever there are breaking changes, you'll be contacted via email to your technical email configured in our dashboard to enable you to update the SDK version to the latest we published.

For more API or info on how it works, don't hesitate to contact our customer support.

The current version is in beta; every bug or problem during the integration will be valuable feedback for us. Please don't hesitate to contact us; your satisfaction is our great reward.

Requirements

We offer our SDK as a pre-compiled binary package as a XCFramework that you can add to your application. We support iOS versions >= 12.

The iOS PubConsent SDK is written in Swift so if your app is written in Objective-C, please make sure that the Always Embed Swift Standard Libraries flag is set to YES.

Add the SDK to your project

The package can be added using CocoaPods or manually.

Using CocoaPods

The package can be added using CocoaPods:

Xcode >= 12 (XCFramework)

  1. If you haven't already, install the latest version of CocoaPods.
  2. Add this line to your Podfile:
Text


Using Swift Package Manager

The iOS SDK is available through Swift Package Manager as a binary library. In order to integrate it into your iOS project follow the instructions below:

  1. Open your Xcode project
  2. Select your project in the navigator area
  3. Select your project in the PROJECT section
  4. Select the Package Dependencies
  5. Click on the + button
  6. Copy the package url https://github.com/pubtech-ai/pubconsent-sdk-apple-os-spm into the search bar
  7. Select the pubconsent-ios-sdk package from the list
  8. Click on Add Package
  9. From the Choose Package Products for the pubconsent-ios-sdk screen click on Add Package

Manually

The package can also be added manually as explained below:

  1. Download and unzip the latest version of our framework for Xcode >= 12: https://cdn.pubtech.ai/pubconsent-sdk-for-publishers/pubconsent-2.0.0-xcframework.zip
  2. In Xcode, select your project.
  3. Then, select your app target.
  4. Click on the General tab.
  5. Scroll down to the Embedded binaries section.
  6. From finder, drag the PubConsent.framework file into the Embedded binaries section.
  7. Make sure the Copy items if needed box is checked and click on finish.

Initialize the SDK

The initialization process will prepare the SDK for interactions with the user and your application. It is important to launch the SDK initialization as soon as possible since this makes possible for the CMP to be showed as soon as possible to the user to ask consent if necessary.

In the AppDelegate, make sure to import the PubConsent module, then call the initialize method and pass your API key:

Swift


Wrapping UIViewController

Note: the setupUI method should be called only from your main/entry UIViewController which in most cases should be once per app launch.

In order for the SDK to be able to display UI elements and interact with the user, you must provide a reference to your main UIViewController. Make sure to import the PubConsent module and call the setupUI method in Swift, setupUIWithContainerController in Objective-C, of the SDK in the viewDidLoad method of your main UIViewController:

Swift


Call the UI directly

In order to make everything work and to display UI elements you must execute the following line of code: PubConsentCMP.shared.setupUI(containerController: viewControllerRepresentable.viewController)

The following is an example of usage:

Swift


Integrate with App Tracking Transparency

For more information on how to integrate the PubConsent CMP with ATT, you can refer to the relevant App Tracking Transparency (iOS 14.5+).

SDK APIs

The following is the struct you must initialize and provide it to the configure method PubConsent.shared.configure

Struct: CmpConfig

Description:

Represents the configuration settings for the Consent Management Platform (CMP).

Properties:

  • id: A string representing the CMP ID (get if from PubTech Dashboard).
  • appName: A string representing the name of the application.
  • debug: A boolean indicating whether the CMP is in debug mode.
  • callbacks: An optional instance of CmpCallbacks containing callback functions.

Methods:

  • init(id:appName:debug:callbacks:): Initializes a new instance of CmpConfig.

Struct: CmpCallbacks

Description:

Contains callback functions for the primary PubConsentCMP events.

Properties:

  • onConsentReadyCallback: A callback function called when consent is ready.
  • onCloseUICallback: A callback function called when the CMP UI is closed.
  • onCmpUIOpenCallback: A callback function called when the CMP UI is opened.
  • onErrorCallback: A callback function called when an error occurs.
  • onGoogleConsentModeCallback: A callback function called when Google consent mode is updated.

PubConsent CMP Methods

The following methods are accessible through the PubConsentCMP.shared instance.

Method: isVendorConsentEnabled(vendorId:)

Description: This method checks whether consent is enabled for a specific vendor.

Parameters:

  • vendorId: An integer representing the ID of the vendor.

Returns:

  • Bool: Returns true if consent is enabled for the specified vendor, otherwise false.

Method: isPurposeConsentEnabled(purposeId:)

Description: This method checks whether consent is enabled for a specific purpose.

Parameters:

  • purposeId: An integer representing the ID of the purpose.

Returns:

  • Bool: Returns true if consent is enabled for the specified purpose, otherwise false.

Method: isFeatureCookiesEnabled()

Description: This method checks whether consent is enabled for feature cookies.

Returns:

  • Bool: Returns true if consent is enabled for feature cookies, otherwise false.

Method: isUserExperienceCookiesEnabled()

Description: This method checks whether consent is enabled for user experience cookies.

Returns:

  • Bool: Returns true if consent is enabled for user experience cookies, otherwise false.

Method: isMeasurementCookiesEnabled()

Description: This method checks whether consent is enabled for measurement cookies.

Returns:

  • Bool: Returns true if consent is enabled for measurement cookies, otherwise false.

Method: getGoogleConsentMode()

Description: This method retrieves the consent mode status for Google services.

Returns:

  • [GoogleConsentModeType: GoogleConsentModeStatus]?: Returns a dictionary containing the consent mode status for different Google services. Returns nil if the consent mode information is not available.

Method: setUserRejectedAll(containerController: viewControllerRepresentable.viewController)

Description:This method will be used for app tracking transparency (ATT) integration. Instead of calling the setupUI method when the ATT state is denied, your app code should execute this method if you need to set everything in the CMP to denied, otherwise you can also choose to have no consent expressed at all.

Method: disableCmpPopup()

Description: This method is going to be used for the App Tracking Transparency (ATT) integration. When the CMP is already showed to the user and the ATT is denied you can disable the CMP popup by hiding it.

Method: enableCmpPopup(containerController: UIViewController)

Description: This method is going to be used for the App Tracking Transparency (ATT) integration. When the ATT is granted you can enable the CMP by calling this method.

Metodo: resetUserPreferences()

Descrizione: This method when called delete all the user preferences relative to the CMP consents.