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]? = nil) -> Bool {
    // Override point for customization after application launch.
    // Note: to set self as the delegate, this class needs to implement the TuneDelegate protocol
    // call one of the Tune init methods
    Tune.initialize(withTuneAdvertiserId: , tuneConversionKey: )
    // Note: only for debugging
    // 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
    // Uncomment this line to enable auto-measurement of successful in-app-purchase (IAP) transactions as "purchase" events
    // 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!) {
    // measure a new session each time the app becomes active
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
    // when the app is opened due to a deep link, call the Tune deep link setter
    Tune.handleOpenURL(url, sourceApplication: sourceApplication)
    return true;

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: \(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() {

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"

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