iOS MCS Implementation
To aid the implementation of the Mobile Consent Solution for iOS
Rebecca avatar
Written by Rebecca
Updated over a week ago

Installation

MobileConsentsSDK is available through the Swift Package Manager (SPM). To start using it you need to add a new Package Dependency to your XCode project with the following repository URL:

https://github.com/cookie-information/ios-release

If you're unable to use SPM in your project, you can add the source code to your project either as a Git submodule or manually copy it into your Xcode Workspace. Using the manual method is discouraged as it requires you to manually update the SDK when security or feature updates are released.

Using the SDK

Initializing

Swift

import MobileConsentsSDK let mobileConsentsSDK = MobileConsents(clientID: "<CLIENT_ID>", clientSecret: "<CLIENT_SECRET>", solutionId: "<SOLUTION ID>" )

Objective-C

@import MobileConsentsSDK; MobileConsents *mobileConsents = [[MobileConsents alloc] initWithUiLanguageCode:@"EN" clientID:@"<CLIENT_ID>" clientSecret:@"<CLIENT_SECRET>" solutionId:@"<SOLUTION ID>" accentColor: UIColor.systemBlueColor fontSet: FontSet.standard ];

Using built-in mobile consent UI

SDK contains built-in screens for managing consent. By default, built-in UI tries to use the application's current language for consent translations. If the application's language is not available in translations, English will be used.

Privacy Pop-Up

To show the Privacy Pop Up screen, use either showPrivacyPopUp (typically used in settings to allow for modification of the consent) or showPrivacyPopUpIfNeeded (typically used at startup to present the privacy screen conditionally. See more below) method:

mobileConsentsSDK.showPrivacyPopUp() { settings in settings.forEach { consent in switch consent.purpose { case .statistical: break case .functional: break case .marketing: break case .necessary: break case .custom: if consent.purposeDescription.lowercased() == "age consent" { // handle user defined consent items such as age consent } @unknown default: break } print("Consent given for:\(consent.purpose): \(consent.isSelected)") } }

The above function takes an optional completion block argument that should be used to react to the user's consent and start or block various third-party SDKs.

By default, the pop-up is presented by top view controller of key window of the application. To change that, you can pass presenting view controller as an optional parameter.

Presenting the privacy pop-up conditionally

The showPrivacyPopUpIfNeeded method is typically used to present the popup aftre app start (or at a point the developer deems appropriate). The method checks if valid consent is already saved on the device and also checks if there are any updates on the Cookie Information server. In case there is no consent saved or the consent version is different from the one available on the server, the popup will be presented, otherwise only the completion closure is called. Using the ignoreVersionChanges parameter allows the developer to turn off the version-checking mechanism and ignore consent version changes coming from the server.

mobileConsentsSDK.showPrivacyPopUpIfNeeded(ignoreVersionChanges: true) { settings in // handle results here }

Objective-C

Just like in Swift, the same methods are used to display the privacy pop-up, only with a slight variation to reflect Objective-C naming conventions.

[self.mobileConsents showPrivacyPopUpIfNeededOnViewController:self animated:YES ignoreVersionChanges:NO completion:^(NSArray<UserConsent *> * _Nonnull) { // Handle consents here }];

Styling

The UI accent colour and the fonts can be customized in the SDKs initializer:

MobileConsents( clientID: "<CLIENT_ID>", clientSecret: "<CLIENT_SECRET>", solutionId: "<SOLUTION ID>" accentColor: .systemGreen, fontSet: FontSet(largeTitle: .boldSystemFont(ofSize: 34), body: .monospacedSystemFont(ofSize: 14, weight: .regular), bold: .monospacedSystemFont(ofSize: 14, weight: .bold)) )

Consent solution description and consent item texts can leverage HTML tags for basic text styling. Supported tags include:

  • <b> for bolding text

  • <i> and <em> for emphasizing text

  • <br> for line breaking

  • <ul> and <li> for creating lists

  • <a href> for embedding links

Basic inline CSS is also supported, e.g. <span style=\"color:red\">Text with custom color</span>

UI language

By default, Privacy Pop-up and Privacy Center use the application's current language for consent translations. If the application's language is not available in consent translations, English is used.

You can override the language used by the screens by initializing SDK with a custom language code. See the example app for more details.

Sending Consent to the server manually

If you want to send consent to the server, first you have to create Consent object which structure looks like this:

var consent = Consent(consentSolutionId: "consentSolution.id", consentSolutionVersionId: "consentSolution.versionId") /* if you want your consent to have a custom data you can add it as a last parametr */ let customData = ["email": "test@test.com", "device_id": "test_device_id"] var consent = Consent(consentSolutionId: "consentSolution.id", consentSolutionVersionId: "consentSolution.versionId" customData: customData)


Then you have to add processing purposes which contain a given consent:

/* given consents are included in main consent object as ProcessingPurpose objects which you can add to Consent object using `addProcessingPurpose` function */ let purpose = ProcessingPurpose(consentItemId: "consentItem.id", consentGiven: {true / false}, language: "en") consent.addProcessingPurpose(purpose)

After setting up the Consent object you are ready to send it to the server

mobileConsentsSDK.postConsent(consent) { error in /* if error is nil it means that post succeeded */ }

Getting locally saved consents data

let savedData:[SavedConsent] = mobileConsentsSDK.getSavedConsents()

SavedConsent object structure

struct SavedConsent { let consentItemId: String let consentGiven: Bool }

Cancelling last post to server request

mobileConsentsSDK.cancel()

If you have any questions about the MCS implementation, take a look at our Mobile FAQ or contact us at support@cookieinformation.com

Did this answer your question?