Info
Content

[iOS] 0. Migration Guide

This guide will help you migrate from the previous version of the CMP SDK to the current version. We'll cover the changes in method names, parameters, and new or deprecated functionality. For Demo Apps examples, please check this link

Please be aware that this version of the CMP SDK were completely rebuilt from scratch, and so it represents a major breaking change, as all the methods were renamed, as were renamed the signatures, also now offering callbacks to almost all of the methods. In all cases, you will need to modify your code and update your dependencies to ensure that your mobile app works as expected.

Repositories

Please note that all of our repositories changed for the iOS SDK. Follow instructions below to find out where to point your dependency manager. 

Also, notice that we do not provide a static version of our custom framework.

Cocoapod

On your Podfile, replace the previous line with the one below:

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

After changing, run this on the command-line:

pod install --repo-update
Swift Package Manager

Our XCFramework package is now hosted on https://github.com/iubenda/cm-sdk-xcframework-v3.

On XCode, go to the menu File > Add Package Dependencied and point it to the URL above.  

Key Migration Points

  1. Delegate pattern: Instead of individual listeners, the new version uses a single delegate protocol (CMPManagerDelegate) for handling events. It contains 4 main events:
    1. 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.
    2. 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.
    3. 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.
    4. didReceiveError
      This is triggered when the SDK operation has thrown any error.
  2. Completion Handlers: Many methods now include completion handlers for asynchronous operations. Update your code to handle these callbacks appropriately.
  3. Consent String: Use exportCMPInfo() instead of getConsentString() to retrieve the consent information.
  4. Vendor and Purpose Consents: The methods to get vendor and purpose consents now return arrays of IDs. You may need to adjust your logic to handle these arrays.
  5. US Privacy String: The getUSPrivacyString() method has been deprecated. If you were using this for CCPA compliance, please note that this method is not available anymore.
  6. Consent Requirement Check: Use the new checkIfConsentIsRequired(completion:) method to automatically determine if consent is needed before showing the consent layer.

Methods and signatures changes

1. Initialization
  • Old: CMPConsentTool(cmpConfig: CmpConfig).initialize()
  • New: CMPManager.shared.setUrlConfig(UrlConfig)
2. Set UI Configuration
  • Old: .withCmpViewControllerConfigurationBlock { ... }
  • New: CMPManager.shared.setWebViewConfig(ConsentLayerUIConfig())
3. Check and Open Consent Layer
  • Old: check({ ... }, isCached: Bool)
  • New: checkWithServerAndOpenIfNecessary(completion: (Error?) -> Void)
  • Old: openView()
  • New: openConsentLayer(completion: (Error?) -> Void)
5. Accept All Consents
  • Old: acceptAll(onFinish: () -> Void)
  • New: acceptAll(completion: (Error?) -> Void)
6. Reject All Consents
  • Old: rejectAll(onFinish: () -> Void)
  • New: rejectAll(completion: (Error?) -> Void)
7. Enable Purposes
  • Old: enablePurposeList([String], onFinish: () -> Void)
  • New: acceptPurposes([String], updatePurpose: Bool, completion: (Error?) -> Void)
8. Disable Purposes
  • Old: disablePurposeList([String], onFinish: () -> Void)
  • New: rejectPurposes([String], updateVendor: Bool, completion: (Error?) -> Void)
9. Enable Vendors
  • Old: enableVendorList([String], onFinish: () -> Void)
  • New: acceptVendors([String], completion: (Error?) -> Void)
10. Disable Vendors
  • Old: disableVendorList([String], onFinish: () -> Void)
  • New: rejectVendors([String], completion: (Error?) -> Void)
11. Get All Purposes
  • Old: getAllPurposes() -> String
  • New: getAllPurposesIDs() -> [String]
12. Get Enabled Purposes
  • Old: getEnabledPurposes() -> String
  • New: getEnabledPurposesIDs() -> [String]
13. Get All Vendors
  • Old: getAllVendors() -> String
  • New: getAllVendorsIDs() -> [String]
14. Get Enabled Vendors
  • Old: getEnabledVendors() -> String
  • New: getEnabledVendorsIDs() -> [String]
15. Check Purpose Consent
  • Old: hasPurposeConsent(String) -> Bool
  • New: hasPurposeConsent(id: String) -> Bool
16. Check Vendor Consent
  • Old: hasVendorConsent(String) -> Bool
  • New: hasVendorConsent(id: String) -> Bool
17. Export CMP String
  • Old: exportCmpString() -> String
  • New: exportCMPInfo() -> String
18. Import CMP String
  • Old: importCmpString(String, completion: (Error?) -> Void)
  • New: importCMPInfo(String, completion: (Error?) -> Void)
  • Old: reset()
  • New: resetConsentManagementData(completion: (Error?) -> Void)

Deprecated Methods:

  • consentRequestedToday() -> Bool
  • isConsentRequired() -> Bool
  • withCloseListener(() -> Void)
  • withOpenListener(() -> Void)
  • withErrorListener((CmpErrorType, String?) -> Void)
  • withOnCMPNotOpenedListener(() -> Void)
  • withOnCmpButtonClickedCallback((CmpButtonEvent) -> Void)
  • withCmpViewControllerConfigurationBlock((UIViewController?) -> Void)
  • withCmpViewConfigurationBlock((UIView) -> Void)
  • withUpdateGoogleConsent(([String: String]?) -> Void)

Note: The new SDK uses a shared instance (CMPManager.shared) and a delegate pattern for callbacks. Implement CMPManagerDelegate for handling events.

Migration examples

Swift

// ============================================
// Previous versions
// ============================================

override func viewDidLoad({
  super.viewDidLoad()
    let vendoradded = NSNotification.Name.CmpConsentVendorAdded;
  NotificationCenter.default.addObserver(self, selector: #selector(handleVendorAdded(notification:)), name: vendoradded, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(handleConsentUpdated(notification:)), name: Notification.Name.CmpConsentConsentChanged, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(handleVendorRemoved(notification:)), name: Notification.Name.CmpConsentVendorRemoved, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(handleConsentUpdated(notification:)), name: Notification.Name.CmpConsentChanged, object: nil)
    setupCmpConfig();

private func setupCmpConfig() {
  let cmpConfig : CmpConfig = CmpConfig.shared.setup(withId: "YOUR_CODE_ID_HERE, domain: myCmpConfig.domain, appName: myCmpConfig.appName, language: myCmpConfig.language);
  cmpConfig.logLevel = CmpLogLevel.verbose;
  cmpManager = CMPConsentTool(cmpConfig: cmpConfig)
    .withErrorListener(onCMPError)
    .withCloseListener(onClose)
    .withOpenListener(onOpen)
    .withOnCMPNotOpenedListener(onCMPNotOpened)
    .withOnCmpButtonClickedCallback(onButtonClickedEvent)
    .withCmpViewControllerConfigurationBlock { viewController in
      viewController?.modalPresentationStyle = .popover
      viewController?.modalTransitionStyle = .crossDissolve
                                              }
  .initialize()
  }

// ============================================
// SDK v3 implementation
// ============================================
                                           
class DemoAppViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        
        let cmpManager = CMPManager.shared
        cmpManager.setUrlConfig(UrlConfig(id: "YOUR_CODE_ID_HERE", domain: "delivery.consentmanager.net", language: "IT", appName: "CMDemoAppSwift"))
        cmpManager.setWebViewConfig(ConsentLayerUIConfig(
            position: .fullScreen,
            backgroundStyle: .dimmed(.black, 0.5),
            cornerRadius: 5,
            respectsSafeArea: true,
            allowsOrientationChanges: true
        ))

        cmpManager.setPresentingViewController(self)
        cmpManager.delegate = self
        cmpManager.checkWithServerAndOpenIfNecessary() { result in
            print("CMPManager initialized and open consent layer opened if necessary")
        }
    }
}

// MARK: - SDK delegates - callbacks
extension DemoAppViewController: CMPManagerDelegate {
    func didChangeATTStatus(oldStatus: Int, newStatus: Int, lastUpdated: Date?) {
        print("DemoApp received a change in the ATTStatus")

    }
    
    func didReceiveError(error: String) {
        print("DemoApp received consent layer error: \(error)")
    }
    
    func didReceiveConsent(consent: String, jsonObject: [String : Any]) {
        print("DemoApp received consent.")
    }
    
    func didShowConsentLayer() {
        print("DemoApp displayed Consent Layer.")

    }
  
    func didCloseConsentLayer()
        print("DemoApp received close consent message.")
        
        let homeView = HomeView()
        let hostingController = UIHostingController(rootView: homeView)
        self.view.window?.rootViewController = hostingController
    }
}

Objective-C 

// ==========================================
// Objective-C v3 implementation
// ==========================================

- (void)initializeConsentManager {
    CMPManager *cmpManager = [CMPManager shared];
    [cmpManager setDelegate:self];
    
    UrlConfig *urlConfig = [[UrlConfig alloc] initWithId:@"YOUR_CODE_ID_HERE"
                                                 domain:@"delivery.consentmanager.net"
                                               language:@"EN"
                                                appName:@"CMDemoAppObjC"];
    [cmpManager setUrlConfig:urlConfig];
    
    ConsentLayerUIConfig *uiConfig = [[ConsentLayerUIConfig alloc] initWithPosition:PositionFullScreen
                                                                    backgroundStyle:BackgroundStyleDimmed
                                                                       cornerRadius:5
                                                                   respectsSafeArea:YES
                                                          allowsOrientationChanges:YES];
    [cmpManager setWebViewConfig:uiConfig];
    
    [cmpManager setPresentingViewController:self];
    
    [cmpManager checkWithServerAndOpenIfNecessary:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"Error initializing CMPManager: %@", error.localizedDescription);
        } else {
            NSLog(@"CMPManager initialized and open consent layer opened if necessary");
        }
    }];
}
Back to top