PubConsent
iOS Integration
28min
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) if you haven't already, install the latest version of cocoapods add this line to your podfile pod 'pubconsent', '3 0 0' 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 open your xcode project select your project in the navigator area select your project in the project section select the package dependencies click on the + button copy the package url https //github com/pubtech ai/pubconsent sdk apple os spm https //github com/pubtech ai/pubconsent sdk apple os spm into the search bar select the pubconsent ios sdk package from the list click on add package 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 download and unzip the latest version of our framework for xcode >= 12 https //cdn pubtech ai/pubconsent sdk for publishers/pubconsent 3 0 0 xcframework zip https //cdn pubtech ai/pubconsent sdk for publishers/pubconsent 3 0 0 xcframework zip in xcode, select your project then, select your app target click on the general tab scroll down to the embedded binaries section from finder, drag the pubconsent framework file into the embedded binaries section make sure the copy items if needed box is checked and click on finish consent sharing with webview if you need to reuse the consent collected through our sdk in a webview pointing to your website, you can refer to the documentation consent sharing with webview docid\ zoopmmq mqsabxypt4cro 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 import swiftui import pubconsentc class appdelegate nsobject, uiapplicationdelegate { func application( application uiapplication, didfinishlaunchingwithoptions launchoptions \[uiapplication launchoptionskey any]? = nil) > bool { let consentreadyhandler = consentreadyhandler() let closeuihandler = closeuihandler() let openuihandler = openuihandler() let googleconsentmodehandler = googleconsentmodehandler() let errorhandler = errorhandler() let cmpcallbacks = cmpcallbacks( onconsentreadycallback consentreadyhandler, oncloseuicallback closeuihandler, oncmpuiopencallback openuihandler, onerrorcallback errorhandler, ongoogleconsentmodecallback googleconsentmodehandler ) let parameters = cmpconfig( id "your id", appname "your app name", debug false, callbacks cmpcallbacks ) pubconsentcmp shared configure(cmpconfiguration parameters) return true } } @main struct yourappname app { @uiapplicationdelegateadaptor(appdelegate self) var appdelegate var body some scene { windowgroup { contentview() } } } class consentreadyhandler onconsentreadycallback { func onconsentready(consentapiinstance any pubconsent consentapiinterface) { if (consentapiinstance getcmptype() == cmptype tcf v2 gdpr) { if let apiinstance = consentapiinstance as? tcfgdprconsentapi { print("google consent status \\(apiinstance isvendorconsentenabled(vendorid 755))") print("google consent mode ad personalization granted? \\(apiinstance getgoogleconsentmode()\[googleconsentmodetype ad personalization] == granted)") } else { print("contact pubtech since there is some unexpected problem (this can't happen but it's better to track every exception ") } } if (consentapiinstance getcmptype() == cmptype google consent mode) { if let apiinstance = consentapiinstance as? gcmconsentapi { print("google consent mode ad personalization granted? \\(apiinstance getgoogleconsentmode()\[googleconsentmodetype ad personalization] == granted)") } } } } class closeuihandler oncloseuicallback { func oncmpuiclosed() { print("cmp ui has been closed ") } } class openuihandler onopenuicallback { func oncmpuiopen() { print("cmp ui has been opened ") } } // implement the ongoogleconsentmodecallback protocol // deprecated will be deleted in v3 0 0 use the api exposed with onconsentready instead class googleconsentmodehandler ongoogleconsentmodecallback { func update(googleconsentmodemap \[googleconsentmodetype googleconsentmodestatus]) { print("google consent mode updated ") print("status ad personalization \\(googleconsentmodemap\[googleconsentmodetype ad personalization] == googleconsentmodestatus granted)") print("status ad storage \\(googleconsentmodemap\[googleconsentmodetype ad storage] == googleconsentmodestatus granted)") print("status ad user data \\(googleconsentmodemap\[googleconsentmodetype ad user data] == googleconsentmodestatus granted)") print("status analytics storage \\(googleconsentmodemap\[googleconsentmodetype analytics storage] == googleconsentmodestatus granted)") // additional code to handle google consent mode updates } } class errorhandler onerrorcallback { func onerror(message string) { print("error occurred \\(message)") } } 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 import swiftui struct viewcontrollerrepresentable uiviewcontrollerrepresentable { let viewcontroller = uiviewcontroller() func makeuiviewcontroller(context context) > some uiviewcontroller { return viewcontroller } func updateuiviewcontroller( uiviewcontroller uiviewcontrollertype, context context) { // no implementation needed nothing to update } } 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 import swiftui import pubconsent import webkit import uikit struct contentview view { private let viewcontrollerrepresentable = viewcontrollerrepresentable() var body some view { zstack { vstack { hstack(alignment bottom, content { vstack(alignment leading, content { text("pubconsent cmp") font( title) text("ios") font( title) colorinvert() }) }) vstack(spacing 30) { text("demo actions ") font( headline) button("open cmp") { pubconsentcmp shared shownotice() } button("print info") { print("google vendor is enabled? \\(pubconsentcmp shared isvendorconsentenabled(vendorid 755))") } } padding( top, 100) background { viewcontrollerrepresentable frame(width zero, height zero) } onappear { task { pubconsentcmp shared setupui(containercontroller viewcontrollerrepresentable viewcontroller) } } } } } } \#preview { contentview() } 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+) docid\ g pmxbpsw shdawu4kqds 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 consentapiinterface using the api exposed through onconsentreadycallback , you can check which of the following implementations are available using the getcmptype() method (this choice depends on the configuration saved through the pubconsent configurator) below, we present the apis for each cmp type we offer the following methods are accessible via the instance consentapiinstance any pubconsent consentapiinterface , available as a parameter in the callback onconsentreadycallback api available for cmptype tcf v2 gdpr 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 api available for cmptype google consent mode the following methods are accessible through by using the instance provided on the onconsentreadycallback 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 pubconsent cmp shared m ethods the following methods are accessible through by using the pubconsent shared instance 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