Set up the Agentedit

Requirementsedit

This project requires Swift 5.3, and is intended for use in Swift-base mobile apps.

Add the Agent dependencyedit

Add the Elastic APM iOS Agent to your Xcode project or your Package.swift.

Here are instructions for adding a package dependency to a standard Xcode poject.

Details of adding dependencies to your Package.swift can be found on Add a Dependency on Another Swift Package. Below is a helpful code-snippet:

package.swift:

Package(
    dependencies:[
        .package(name: "iOSAgent", url: "git@github.com:elastic/apm-agent-ios.git", .branch("main")),
    ],
  targets:[
    .target(
        name: "MyApp",
        dependencies: [
            .product(name: "iOSAgent", package: "iOSAgent")
        ]
    ),
])

Initialize the agentedit

Once the Agent has been added as a dependency, it must be intialized.

If you’re using SwiftUI to build your app add the following to your App.swift:

import SwiftUI
import iOSAgent

@main
struct MyApp: App {
    init() {
        var config = AgentConfiguration()
        config.collectorAddress = "127.0.0.1" 
        config.collectorPort = 8200 
        config.collectorTLS = false 
        config.secretToken = "<secret token>" 
        Agent.start(with: config)
    }
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

APM Server URL or IP address

APM Server port number

Enable TLS for Open telemetry exporters

Set secret token for APM server connection

If you’re not using SwiftUI you can alternatively add the same thing to your AppDelegate file:

AppDelegate.swift

import UIKit
import iOSAgent
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        var config = AgentConfiguration()
        config.collectorAddress = "127.0.0.1" 
        config.collectorPort = 8200 
        config.collectorTLS = false 
        config.secretToken = "<secret token>" 
        Agent.start(with: config)
        return true
    }
}

APM Server url or ip address

APM Server port number

Enable TLS for Open telemetry exporters

Set secret token for APM server connection