How to Invoke the Device Data Collector from a Swift App using iOS SDK 4.1.x

Provide native support for your Kount solution by integrating Kount iOS SDK 4.1.x into your Swift iOS (version 11 or later) application. This allows you to use Kount's service without having to create API calls. Software for this integration can be found in GitHub.

Note: This integration document is for iOS versions 13 or later. If the application is using segues, then the analytics data only captures for that view controller if the modal is presented using UIModalPresentation.fullScreen. For iOS versions 12 or earlier, the default presentation style is already full screen, therefore analytics data already captures for those screens.

Invoking the Kount iOS SDK into a Swift App

  1. Copy libKountDataCollector.a from the Kount SDK and add it in iOS project. Place KountDataCollectorUniversalLibrary folder from Kount SDK inside the folder structure of iOS project.
  2. Add h and KountAnalyticsViewController.h from KountDataCollectorUniversalLibrary inside the iOS project:
    1. Go to the Project Navigator view.
    2. Right-click your app’s project name, and then select Add files to “{Project}”.
    3. From the Finder window, select KountDataCollectorUniversalLibrary.
    4. Under KountDataCollectorUniversalLibrary, select kDataCollector.h and KountAnalyticsViewController.h, and then click Add.
    5. Xcode asks to create a bridging header. Click the option to create the header.
  3. To manually add the bridging header:
    1. Go to Project Target, Build Settings, and then Header Search Paths.
    2. Add Header search path as ../KountDataCollectorUniversalLibrary.
    3. For the Bridging header name, use <Target-name>-Bridging-Header.h.
  4. Add following import statements in the bridging header file:
    #import "KDataCollector.h"

    #import "KountAnalyticsViewController.h"
  5. Add the folder for touch event related files (for reference) under Project target.
    1. Go to the Project Navigator view.
    2. Right-click the app’s project name, and then select Add files to “{Project}”.
    3. From the Finder window, select KountDataCollectorUniversalLibrary.
    4. Under KountDataCollectorUniversalLibrary, select the TouchControls folder , which contains touch event related files, and then click Add.
  6. For iOS 13 and later
    Under Scenedelegate.swift, in sceneDidEnterBackground method, add:
    KountAnalyticsViewController().registerBackgroundTask()
    For versions iOS 12 and earlier
    Under AppDelegate.swift, in ApplicationDidEnterBackground method, add:
    KountAnalyticsViewController().registerBackgroundTask()
  7. In Appdelegate.swift, write the following code example:
    // Global Variable
    var sessionID : String = ""
    // In method didFinishLaunchingWithOptions, replace their old Kount configuration related code with following code:
    KDataCollector.shared().debug = true
    // Set your Merchant ID
    KDataCollector.shared().merchantID = 99999 // Insert your valid merchant ID
    // Set the location collection configuration
    KDataCollector.shared().locationCollectorConfig =
    KLocationCollectorConfig.requestPermission
    // For a released app, you'll want to set this to KEnvironment.Production
    KDataCollector.shared().environment = KEnvironment.test
    KountAnalyticsViewController.setEnvironment(KDataCollector.shared().environment)
    KDataCollector.shared().environment = KEnvironment.test        KountAnalyticsViewController().setEnvironmentForAnalytics(KDataCollector.shared().environment)
    // To collect Analytics Data, set Boolean flag - analyticsData to true.
    let analyticsData = true
    KountAnalyticsViewController.collect(sessionID: analyticsSwitch: analyticsData) {
        (sessionID, success, error) in
        //Completion block to know whether device data collection is successful/failed with error/failed without error.
        if (success) {
            print("Collection Successful")
        }
    
        else {
            if ((error) != nil) {
                print("Collection failed with error: ",error!.localizedDescription)
            }
            else {
                print("Collection failed without error")
            }
        }
    
    }
    The KountAnalyticsViewController keeps the Session ID in a global variable to be retrieved at any time, named as KountAnalyticsViewController.getAppSessionID.
  8. In the view controllers where you want to detect events, inherit view controller from KountAnalyticsViewController and remove UITextFieldDelegate (if any).
    Note: If your view controller does not inherit directly from UIViewController, you must perform this step on the controller that does inherit from UIViewController, and ensure that controller calls super.init().
  9. In the view controller where you want to capture analytics data, under ViewDidLoad(), add:
    <textfield_Name>.delegate = self.
    It must be added for all textfields on that page.
  10. In the login view controller, inside the login action method, add:
    Inside code for Successful login, add:
    KountAnalyticsViewController().storeLog(inEvents: true)
    Inside code for Unsuccessful login, add:
    KountAnalyicsViewController().storeLog(inEvents: false)
  11. If a ViewController in application is overriding the method that we are already using for capturing analytics data, then call super <MethodName> in the ViewController method. This step involves some client-side coding if there are multiple methods, but these will only be one line of code for such methods.
  12. Almost all Kount API calls require your Kount assigned merchantID and a sessionID (KountAnalyticsViewController.getAppSessionID). You must send the getAppSessionID you created earlier back to your servers. This can be done at any time prior to the Kount API calls being made (from your servers to ours). For example, if you are using Kount Control and plan to evaluate logins, you could include the sessionID along with the users login credentials to your servers. This sessionID is accessible as a global variable. Refer to the Kount Support website for further documentation.
  13. Build and Run.
Was this article helpful?
0 out of 0 found this helpful