AppChains Integration using Swift | Sequencing.com

Real-Time Personalization (+RTP) API Guide

Integration

Sequencing.com has already everything to simplify integration of AppChains API into your Swift based project.

There are 2 ways of achiving this:

  • Get code from our GitHub repository and paste it into your project
  • Integrate using our Cocoapod artifacts

For Cocoapods integration please follow next steps

1) If you are new to CocoaPods, check general "Getting started" guide

2) Create Podfile in your project directory:

$ pod init

3) Specify following parameters in Podfile:

pod 'sequencing-app-chains-api-swift', '~> 2.1.1'

4) Install the dependency in your project:

$ pod install

5) Always open the Xcode workspace instead of the project file.

$ open *.xcworkspace

6) Create bridging header file. Select File > New > File > Header File > name it as

project-name-Bridging-Header.h

7) Add AppChains class import in the bridging header file

#import <AppChainsLibrary/AppChains.h>

8) Register your bridging header file in the project settings. Select your project > project target > Build Settings > Objective-C Bridging Header specify path for bridging header file

$(PROJECT_DIR)/project-name-Bridging-Header.h

Swift module API

Method Purpose Arguments Description
init(token: String, withHostName: String) Constructor  
func getReportWithApplicationMethodName(applicationMethodName: String!, withDatasourceId: String!, withSuccessBlock: ((Report!) -> Void)!, withFailureBlock: ((NSError!) -> Void)!) Constructor
  • applicationMethodName - name of data processing routine
  • datasourceId - input data identifier
  • success - callback executed on success operation, results with Report object
  • failure - callback executed on operation failure
Constructor used for creating AppChains class instance in case reporting API is needed and where security token is required
func getBatchReportWithApplicationMethodName(appChainsParams: [AnyObject]!, withSuccessBlock: ReportsArray!, withFailureBlock: ((NSError!) -> Void)!) Reporting API with batch request
  • appChainsParams - array of params for batch request.
    Each param should be an array with 2 items:
    first object: applicationMethodName
    last object: datasourceId
  • success - callback executed on success operation, results with ReportsArray as array of dictionaries.
    Each dictionary has following keys and objects:
    appChainID - appChain ID string
    report - Report object
  • failure - callback executed on operation failure
 

 

Code snippet

Single App Chain retrieval example

let appChainsManager = AppChains.init(token: accessToken as String, withHostName: "api.sequencing.com")
appChainsManager.getReportWithApplicationMethodName("<chain id>", withDatasourceId: "<file id>", withSuccessBlock: { (result) in
        let resultReport: Report = result as Report!
        
        if resultReport.isSucceeded() {    
            if resultReport.getResults() != nil {
                for item: AnyObject in resultReport.getResults() {
                    
                    let resultObj = item as! Result
                    let resultValue: ResultValue = resultObj.getValue()
                    
                    if resultValue.getType() == ResultType.Text {
                        print(resultObj.getName() + " = " + (resultValue as! TextResultValue).getData())
                    }
                }
            }
        } else {
            print("Error occured while getting genetic information")
        }
        
    }) { (error) in
        print("Error occured while getting genetic information. " + error.localizedDescription)
    }

 

Batch App Chain retrieval example

let appChainsManager = AppChains.init(token: accessToken as String, withHostName: "api.sequencing.com")
let appChainsForRequest: NSArray = [ [ "<chain id>", "<file id>"], ["<chain id>", "<file id>" ] ]

appChainsManager.getBatchReportWithApplicationMethodName(appChainsForRequest as [AnyObject], withSuccessBlock: { (resultsArray) in
        let reportResultsArray = resultsArray as NSArray
        for appChainReport in reportResultsArray {
            let appChainReportDict = appChainReport as! NSDictionary
            let resultReport: Report = appChainReportDict.objectForKey("report") as! Report;
            let appChainID: NSString = appChainReportDict.objectForKey("appChainID") as! NSString;
            
            print(appChainID)
            
            if resultReport.getResults() != nil {
                for item: AnyObject in resultReport.getResults() {
                    
                    let resultObj = item as! Result
                    let resultValue: ResultValue = resultObj.getValue()
                    
                    if resultValue.getType() == ResultType.Text {
                        print(resultObj.getName() + " = " + (resultValue as! TextResultValue).getData())
                    }
                }
            }  
        }
            
    }) { (error) in
        print("batch request error. " + error.localizedDescription)
        completion(appchainsResults: nil)
    }