Including Tune iOS Objective-C SDK in a Swift project

If you’re creating an iOS project using the Swift programming language in Xcode 6.4+, you can use the existing Tune SDK 4.x.x for iOS (which is based on the Objective-C SDK framework).

To include the Tune SDK for iOS:

  • In Xcode Project Navigator panel, drag-and-drop the Tune.framework on the project name.

You can import Tune module and other required frameworks in AppDelegate and in any other class where you want to measure events.

import Tune
 
import AdSupport
import CoreTelephony
import iAd
import MobileCoreServices
import Security
import StoreKit
import SystemConfiguration
import UIKit

Add the following flags to “Other Linker Flags” in the Xcode project Build Settings.

-ObjC -lz

In AppDelegate.swift file, add the following:

let Tune_Advertiser_Id   = "your_tune_advertiser_id"
let Tune_Conversion_Key  = "your_tune_conversion_key"
 
 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any] ? ) - > Bool {
        // Override point for customization after application launch.

        // Note: to set self as the delegate, this class needs to implement the TuneDelegate protocol
        Tune.setDelegate(self)

        // call one of the Tune init methods
        Tune.initialize(withTuneAdvertiserId: Tune_Advertiser_Id, tuneConversionKey: Tune_Conversion_Key)
        
        // Note: only for debugging
        //Tune.setDebugMode(true)

        // Check if a deferred deeplink is available and handle opening of the deeplink as appropriate in the success tuneDidReceiveDeeplink: callback.
        // Uncomment this line if your TUNE account has enabled deferred deeplinks
        //Tune.checkForDeferredDeeplink(self)

        // Uncomment this line to enable auto-measurement of successful in-app-purchase (IAP) transactions as "purchase" events
        //Tune.automateIapEventMeasurement(true)

        // If your app already has a pre-existing user base before you implement the Tune SDK, then
        // identify the pre-existing users with this code snippet.
        // Otherwise, TUNE counts your pre-existing users as new installs the first time they run your app.
        // Omit this section if you're upgrading to a newer version of the Tune SDK.
        // This section only applies to NEW implementations of the Tune SDK.
        //var isExistingUser:Bool = ...
        //if (isExistingUser) {
        //    Tune.setExistingUser(true)
        //}

        return true
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

        Tune.measureSession()
    }

    func application(_ application: UIApplication, open url: URL, sourceApplication: String ? , annotation : Any) - > Bool {
        // when the app is opened due to a deep link, call the Tune deep link setter
        Tune.handleOpen(url, sourceApplication: sourceApplication)

        return true;
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
}

To receive TUNE server responses, implement the TuneDelegate protocol by defining an extension outside the AppDelegate class body:

extension AppDelegate: TuneDelegate {
    func tuneDidSucceed(with data: Data!) {
        let str = String(data: data, encoding: String.Encoding.utf8)
        print("Tune success: \(String(describing: str))")
    }

    func tuneDidFailWithError(_ error: Error!) {
        print("Tune failed: \(error)")
    }

    func tuneEnqueuedRequest(_ url: String!, postData post: String!) {
        print("Tune request enqueued: \(url), post data = \(post)")
    }
}

To measure an event, the measureEvent method can be used, e.g.

event without event items

func loginComplete() {
    Tune.measureEventName("login")
}

event with event items

func purchaseComplete() {
    let item1 = TuneEventItem(name: "ball", attribute1: "red", attribute2: "medium", attribute3: "", attribute4: "", attribute5: "")
    let item2 = TuneEventItem(name: "ball", attribute1: "blue", attribute2: "large", attribute3: "", attribute4: "", attribute5: "")
    
    let event = TuneEvent(name: "purchase")
    event.eventItems = [item1, item2]
    event.refId = "1234567890"
    
    Tune.measure(event)
}

Using TUNE iOS SDK in Hybrid with Swift/Objective-C App

To use Tune to measure session from Swift code, (if your app is mostly in Objective-C with few Swift classes) you can create a bridging header and import Tune SDK to measure an event from the Swift code.

For more information on this topic, see the Importing Objective-C into Swift section in the Apple Developers Swift and Objective- C in the Same Project article.

No Comments

Leave a reply