Set up the Agent

edit

Requirements

edit

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

Other platform requires:

platform version

iOS

11

macOS

10.13

tvOS

v11

watchOS

3

Add the Agent dependency

edit

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: "apm-agent-ios", url: "https://github.com/elastic/apm-agent-ios.git", from: "1.0.0"),
    ],
  targets:[
    .target(
        name: "MyApp",
        dependencies: [
            .product(name: "ElasticApm", package: "apm-agent-ios")
        ]
    ),
])

Initialize the agent

edit

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

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

import SwiftUI
import ElasticApm

class AppDelegate : NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        var config = AgentConfigBuilder()
            .withServerUrl(URL(string:"http://127.0.0.1:8200")) 
            .withSecretToken("<SecretToken>") 
            .build()

        ElasticApmAgent.start(with: config)
        return true
    }
}

@main
struct MyApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    init() {
    }
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

APM Server URL

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 ElasticApm
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
var config = AgentConfigBuilder()
                       .withServerUrl(URL(string:"http://127.0.0.1:8200")) 
                       .withSecretToken("<SecretToken>") 
                       .build()
        ElasticApmAgent.start(with: config)
        return true
    }
}

APM Server URL

Set secret token for APM server connection