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

Implementing the Device Data Collector (DDC) is a two-step process. If you have not completed the first step of integration, refer to the following articles below:

Provide native support for your solution by integrating the iOS SDK into your Swift iOS (version 11 or later) application. This allows you to use our 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, meaning that analytics data already captures for those screens.

Invoking the Device Data Collector iOS SDK into a Swift App

  1. If you are using Universal Static Library, add .h files 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 select Add.

    5. Xcode asks to create a bridging header. Select the option to create the header.

  2. To manually add the bridging header, use <Target-name>-Bridging-Header.h.

  3. Add following import statements in the bridging header file:

        #import "KDataCollector.h"
        #import "KountAnalyticsViewController.h"
  4. 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 project name, and then select Add files to “{Project}” or move the TouchControls folder to the root of the project in Xcode.

    3. From the Finder window, select KountDataCollectorUniversalLibrary.

    4. Under KountDataCollectorUniversalLibrary, select the TouchControls folder, which contains touch event related files.

    5. Select Copy items if needed, Create groups, and then select one or more target destinations for the analytics.

    6. Select Add.

  5. For iOS 13 and later Under Scenedelegate.swift, in sceneDidEnterBackground method, add:

    KountAnalyticsViewController().registerBackgroundTask()

    For versions iOS 12 and earlierUnder AppDelegate.swift, in ApplicationDidEnterBackground method, add:

    KountAnalyticsViewController().registerBackgroundTask()
  6. 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 = "900900"
    // 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().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.

    Optional:

    If you want to see the status of the DDC, add the following to your code:

        getKDataCollectionStatus()
        getKDataCollectionError()
  7. In the view controllers where you want to detect events, inherit the 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 the controller calls super.init().

  8. 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.

  9. In the login view controller, inside the login action method:

    Inside the code for Successful login, add:

    KountAnalyticsViewController().storeLog(inEvents: true)

    Inside the code for Unsuccessful login, add:

    KountAnalyicsViewController().storeLog(inEvents: false)
  10. If a ViewController in the application is overriding the method you 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.

  11. Almost all API calls require your assigned merchantID and a sessionID (KountAnalyticsViewController.getAppSessionID). Send the getAppSessionID you created back to your servers. This can be done at any time prior to the API calls being made. For example, if you are using our services and plan to evaluate logins, you could include the sessionID along with the user's login credentials to your servers. This sessionID is accessible as a global variable.

  12. Build and Run.

Was this article helpful?
0 out of 3 found this helpful