visionOS

Learn how to use Sentry's Apple SDK to automatically report errors and exceptions in your application.

On this page, we get you up and running with Sentry's Apple SDK, which will automatically report errors and exceptions in your application.

In addition to capturing errors, you can monitor interactions between multiple services or applications by enabling tracing.

Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.

Sentry captures data by using an SDK within your application's runtime. These are platform-specific and allow Sentry to have a deep understanding of how your application works.

We recommend installing the SDK with Swift Package Manager (SPM), but we also support alternate installation methods. To integrate Sentry into your Xcode project, open your App in Xcode and open File > Add Packages. Then add the SDK by entering the git repo url in the top right search field:

Copied
https://github.com/getsentry/sentry-cocoa.git

To capture all errors, we recommend to initialize the SDK on the main thread as soon as possible, such as in your AppDelegate application:didFinishLaunchingWithOptions method:

Copied
import Sentry

func application(_ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    SentrySDK.start { options in
        options.dsn = "___PUBLIC_DSN___"
        options.debug = true // Enabled debug when first installing is always helpful

        // Adds IP for users.
        // For more information, visit: https://docs.sentry.io/platforms/apple/data-management/data-collected/
        options.sendDefaultPii = true
        // ___PRODUCT_OPTION_START___ performance

        // Set tracesSampleRate to 1 to capture 100% of transactions for performance monitoring.
        // We recommend adjusting this value in production.
        options.tracesSampleRate = 1
        // ___PRODUCT_OPTION_END___ performance
        // ___PRODUCT_OPTION_START___ profiling

        options.configureProfiling = {
            $0.lifecycle = .trace
            $0.sessionSampleRate = 1
        }
        // ___PRODUCT_OPTION_END___ profiling
        // ___PRODUCT_OPTION_START___ logs

        // Enable logs to be sent to Sentry
        options.experimental.enableLogs = true
        // ___PRODUCT_OPTION_END___ logs
    }

    return true
}

Verify that your app is sending events to Sentry by adding the following snippet, which includes an intentional error. You should see the error reported in Sentry within a few minutes.

Copied
import Sentry

do {
    try aMethodThatMightFail()
} catch {
    SentrySDK.capture(error: error)
}

Can I initialize the SDK from a background thread?

Yes, you can initialize the SDK from a background thread, but this is not recommended. Some initialization steps, like collecting view hierarchy information, must still run on the main thread. Initializing on the main thread also ensures reliable detection of app launch crashes.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").