• EnglishEspañol日本語한국어Português
  • Log inStart now

Adding an AppDelegate to your SwiftUI app

SwiftUI projects do not generate with an AppDelegate file, which is a necessary component to install and configure New Relic functionality with iOS/tvOS applications. To add an AppDelegate to your SwiftUI App, follow the steps detailed here. Before adding the AppDelegate, follow the outlined procedure to correctly install the New Relic agent.

  1. Add a new file to your project named AppDelegate

  2. Then in that file add a class named AppDelegate that inherits from NSObject and conforms to the UIApplicationDelegate protocol.

  3. In the AppDelegate class you created, add the application:didFinishLaunchingWithOptions function.

  4. As close to the start of application:didFinishLaunchingWithOptions as possible add NewRelic.start(withApplicationToken: "APP_TOKEN") replacing APP_TOKEN with your application token.


    To ensure proper instrumentation, you must call the agent on the first line of didFinishLaunchingWithOptions(), and run the agent on the main thread. Starting the call later, on a background thread, or asynchronously can cause unexpected or unstable behavior.

    import UIKit
    import NewRelic
    class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    NewRelic.start(withApplicationToken: "APP_TOKEN")
    return true
  5. In the main app structure object add the following line: @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate.

    struct SwiftUI_ExampleApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

That is all you need to do to add an AppDelegate.swift file to your SwiftUI application and start the New Relic iOS agent in the recommended way.

Did this doc help with your installation?

Copyright © 2024 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.