Setting Up Your Application in SwiftUI

In a SceneKit based application, this is how you configure TelemetryClient

For SwiftUI plus SceneKit, I recommend adding the initialization to your @main App struct! Open YourAppNameApp.swift and look for code that looks like this:

import SwiftUI

@main
struct Example_AppApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

This is the entry point to your app. Now let's add the initialization here.

Import the TelemetryClient Package by adding import TelemetryClient. Then add an init() method to your App struct that creates a TelemetryManagerConfiguration instance and hands it to the TelemetryManager, using the Unique Identifier of your app that you copied into your clipboard earlier. If you don't have that any more, you can get it at any time from the Telemetry Viewer app.

Your code should now look like this:

import SwiftUI
import TelemetryClient

@main
struct Example_AppApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }

    init() {
        let configuration = TelemetryManagerConfiguration(
            appID: "YOUR-APP-UNIQUE-IDENTIFIER")
        TelemetryManager.initialize(with: configuration)
    }
}

If you already have an init() method, add this to its end.

Now step over to Step 6!

If you use an App Delegate

If you use AppDelegate to manage your app's life cycle, open the file AppDelegate.swift and look for the method application(:didFinishLaunchingWithOptions:). It will probably look similar to this:

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    // ...
}

By default, Xcode even adds a comment here to tell you where to add stuff that should happen right after launch.

Now, import the TelemetryClient package and configure the TelemetryManager using the Unique Identifier of your app that you copied into your clipboard earlier. If you don't have that any more, you can get it at any time from the Telemetry Viewer app.

import UIKit
import TelemetryClient

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        let configuration = TelemetryManagerConfiguration(
            appID: "YOUR-APP-UNIQUE-IDENTIFIER")
        TelemetryManager.initialize(with: configuration)

        return true
    }
    // ...
}

If you already have code in this function, add the two new lines to the end.

You did it!

You've finished setting up your application and are now ready to send some signals.

Next Up: Sending Signals →

Didn't Find Your Answer?

Join the Slack Issues on Github More Docs