Skip to content

iOS Instructions

Maria Makantasi edited this page Dec 8, 2023 · 17 revisions

Cocoapods

Prerequisites:

In your pod file add the following

target 'your_application' do
 use_frameworks!
 platform :ios, '15.2'
 pod 'mobilesdk', :git => 'https://github.com/persado/eapi-mobile-sdk.git', :tag => 'latest_mobilesdk_version', :subspecs => ['type_of_build']
end

Note: type_of_build could be:

  • Release
  • Debug
  • ReleaseSimulator
  • DebugSimulator
    This has to do with what is the reason why you are using the library at each point. This is available from version 0.3.0 and above Through terminal go to your_application base path and install the cocoapods with the following command:
pod install

Or update them if already installed :

pod update

Inside your application add :

import mobilesdk

Note: In podDebug, podDebugSimulator branches there are pod frameworks builded for debug or simulator usage you can access them with the following changes. This should be used for versions minor than 0.3.0. Instructions for versions equal or major than 0.3.0 could be found above

target 'your_application' do
 use_frameworks!
 platform :ios, '15.2'
 pod 'mobilesdk', :git => 'https://github.com/persado/eapi-mobile-sdk.git', :branch => 'podDebugSimulator'
end

Manual Framework installation

Prerequisites :Xcode

  • Download the valid framework for your case (debug, release or debug-simulator) from here
  • Frameworks are build for iOS devices with version 15.2
  • Go to your app "Build Phases" tab and add the framework in "Link Binary with Libraries" section
  • Open "Build Settings" and in the "Framework search Paths" add the path where the downloaded framework is located
  • Open "General" tab in the "Frameworks, Libraries, and Embedded Content select Embed & Sign"
  • Import the framework in your class
     import mobilesdk

Initialize

Overview

You must initialize the PSDClient in your applicaton. PSDClient needs an application identifier ("app-id") which will be given to users by Persado PSDClient can be initialized only asynchronously. Asynchronous happens in the background without delaying other processing. You can access the content as long as it is ready.

Errors during initialization

Errors can be accessible from the callback method.

  • Internet connection was not available [timestamp]
  • Persado unavailable to initialize sdk
  • No Persado campaigns configured for requested app id
  • Something went wrong during Persado sdk initialization [cause]

Example

   /* Optional values in PSDClient builder are:
     1. env : enum Environment (LOCAL, DEV, QA, QAAWS, STG, PROD) default value PROD.
        sets the PersadoAPI environment
     2. qaMode : Boolean default value false -> fetch LIVE and EXPIRED campaigns
        ability to get campaigns that are in DRAFT, PENDING and SCHEDULED state
     3. userAttributes : Map<String,String> default value empty map. Map key value should follow a naming convention. Accepted key values should only contain alphanumeric characters and _ -> available from version 0.4.0,
        gives the ability to pass user attributes that are used in content determination for specific campaign types
    */
    PSDClient.Builder(appId: "app-id").build().initialize{success , error in
                if(success) as! Bool{
                    /*content method here*/
                }else{
                    /* errorMessage will contain one of the error messages described above */
                    log(errorMessage)
                }
   }
// example with user attributes
   PSDClient.Builder(appId: "app-id").userAttributes(value: ["loyalty_points" : "900"]).build().initialize{success , error in
                if(success) as! Bool{
                    /*content method here*/
                }else{
                    /* errorMessage will contain one of the error messages described above */
                    log(errorMessage)
                }
   }

How to use

Serving

Overview

This is a simple example that just gets the text content served from PersadoAPI regardless and sets it as a value to a text view. In different content types the usage of the returned content might differ. During initialization all campaigns that are bundled to the given app-id are stored in the application storage. There are two ways to identify which campaigns you want to work with:

  • Campaign identifier: unique campaign identifier given by the system
  • Campaign label: user friendly name given during the campaign configuration (ex. december_discount)
You can access the storage as you can see in the example below in order to get campaign identifiers or the campaign labels.
Example
    let campaignIdentifiers = PSDContent().getPSDResponse()?.variantResponseMap.keys
    let campaignLabels = PSDContent().getCampaignLabels()

From Dictionary.Keys campaignIdentifiers (this value could be nil) you can select an identifier and find the touchpoints available for this.

   let touchpoints = PSDContent().getCampaignContent(campaignId:"campaign_identifier")?.touchpoints
   let touchpoints = PSDContent().getCampaignContentByLabel(campaignLabel:"campaign_label")?.touchpoints

Set content to text view example:

func setPersadoContent() -> Void {
    // campaign identifier
    let touchpoint : TouchpointVariantResponse? =  PSDContent().getTouchpointContent(campaignId: "campaign_identifier", touchpointName: "name")
    // campaign label
    let touchpoint : TouchpointVariantResponse? =  PSDContent().getTouchpointContentByLabel(campaignLabel: "campaign_label", touchpointName: "name")

    if touchpoint != nil{
        self.content  = (touchpoint?.content)!
    }
}

//asynchronous call the method as a callback in initialization
    PSDClient.Builder(appId: "your app-id").build().initialize{success, errorMessage in
            if(success) as! Bool{
              self.setPersadoContent()
            }
    }

Tracking

Overview

There is the ability to send tracking events (VIEW,CLICK,CONVERT) to PersadoAPI. This feature is available only for:

  • Campaigns that have been initialized for the given application identifier(key)
  • Campaign that have the tracking feature enabled
  • Users that has the tracking enabled on their device.
    For this step if we are on a iOS device with a version greater than 14 then it is requires to implement permission request in the main application
Permission request example

First we need to set Privacy - Tracking Usage Description in the Info.plist. Give as valuethe message than we want to be displayed when the tracking request dialog appears

 func requestPermission() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            if #available(iOS 14, *) {
                ATTrackingManager.requestTrackingAuthorization { status in
                    switch status {
                    case .authorized:
                        // Tracking authorization dialog was shown
                        // and we are authorized
                        print("Authorized")
                    case .denied:
                        // Tracking authorization dialog was
                        // shown and permission is denied
                        print("Denied")
                    case .notDetermined:
                        // Tracking authorization dialog has not been shown
                        print("Not Determined")
                    case .restricted:
                        print("Restricted")
                    @unknown default:
                        print("Unknown")
                    }
                }
            }
        }
    }

As long as the PSDClient has initialized the Persado campaigns then tracking events can be send for a specific campaign (defined by campaign identifier or campaign label).

Errors during tracking

Errors can be accessible from the callback method.

  • Internet connection was not available [timestamp]
  • Persado unavailable to track event
  • No Persado campaigns configured for given campaign identifier
  • Tracking is disabled for given campaign identifier
  • User opted out of tracking
  • Something went wrong during Persado sdk [cause]
Example
   /* Optional value in PSDTrack :
      * env : enum Environment (LOCAL, DEV, QA, QAAWS, STG, PROD) default value PROD.
        sets the PersadoAPI environment
      * userAttributes : Map<String,String> default value empty map. Map key value should follow a naming convention. Accepted key values should only contain alphanumeric characters and  -> available from version 0.4.0,
        gives the ability to pass user attributes used for model training
     Parameters
     trackAction: enum TrackAction CLICK, CONVERT, VIEW)
     campaign_identifier: String 
    */
   func track() -> Void {
     PSDTrack().track(action: .click, campaignId: "campaign_identifier")
     //with env and attributes
     PSDTrack().env(env: .local)..userAttributes(value: ["loyalty_points" : "900"]).track(action: .view, campaignId: "campaign_identifier")
   }
   //access the errorMessages
   func track() -> Void {
     PSDTrack().track(action: .click, campaignId: "campaign_identifier"){
         success, error in
                if(success) as! Bool{
                   log("Tracking Successful")
                }else{
                   log(error)
                }
     }
   }   
// Given the examples above you can use campaign label instead of campaign identifier
     PSDTrack()
        .trackByCampaignLabel(action: .click, campaignLabel: "campaign_label")
     PSDTrack().trackByCampaignLabel(action: .click, campaignLabel: "campaign_label"){
         success, error in
                if(success) as! Bool{
                   log("Tracking Successful")
                }else{
                   log(error)
                }
     }
//asynchronous call the method as a callback in initialization
    PSDClient.Builder(appId: "app-id").build().initialize{success, error in
       if(success) as! Bool{
         self.track()
       }
    }