This documentation contains work-in-progress information for future Elastic Stack and Cloud releases. Use the version selector to view supported release docs. It also contains some Elastic Cloud serverless information. Check out our serverless docs for more details.
Set up the Agent
editSet up the Agent
editRequirements
editThis project requires Swift 5.7
, and is intended for use in Swift-base mobile apps.
Other platform requires:
platform | version |
---|---|
|
|
|
|
|
|
|
|
Add the Agent dependency
editAdd 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
editOnce 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() } } }
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 } }