Our next-gen architecture is built to help you make sense of your ever-growing data. Watch a 4-min demo video!

Back to All Docs

RUM iOS Monitoring Setup

Last Updated: Jul. 02, 2024

This guide provides instructions for integrating the Coralogix RUM SDK into iOS applications for UIKit and SwiftUI projects. It also details the configuration options, logging functionalities, and best practices for using method swizzling and SwiftUI modifiers to monitor and analyze app performance and user interactions.

Prerequisites

  • iOS Version: 13 or higher (Note: SwiftUI screen navigation tracking is available from iOS 13)
  • Swift Compatibility: 5.7 or higher
  • Xcode Version: 14 or higher

Features

The SDK captures the following types of telemetry data:

  • HTTP Requests: Instrumentation using URLSession.
  • Unhandled Exceptions: Including NSException, NSError, and Error.
  • Custom Logs
  • Crashes: Using PLCrashReporter.
  • Page Navigation: Using method swizzling for UIKit and modifiers for SwiftUI.

Installation

To add the Coralogix SDK to your project using Swift Package Manager:

  1. Open File > Add Packages
  2. Search for: git@github.com:coralogix/cx-ios-sdk
  3. Select “Up to Next Major Version”.

Initialization

Ensure to call the SDK as early in your application lifecycle as possible, ideally in applicationDidFinishLaunching in AppDelegate.

Using UIKit (AppDelegate)

import UIKit
import CoralogixRum

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    var coralogixRum: CoralogixRum?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        let options = CoralogixExporterOptions(
            coralogixDomain: CoralogixDomain,
            userContext: nil,
            environment: "ENVIRONMENT",
            application: "APP-NAME",
            version: "APP-VERSION",
            publicKey: "API-KEY",
            ignoreUrls: [],
            ignoreErrors: [],
            customDomainUrl: "",
            labels: ["String": Any],
            debug: false
        )
        self.coralogixRum = CoralogixRum(options: options)
        return true
    }
}

Using SwiftUI

For SwiftUI projects (which don’t include AppDelegate or SceneDelegate files), you must create one to use Coralogix in your app.

import SwiftUI
import CoralogixRum

@main
struct DemoAppApp: App {
    @State private var coralogixRum: CoralogixRum

    init() {
        let options = CoralogixExporterOptions(
            coralogixDomain: CoralogixDomain,
            userContext: nil,
            environment: "ENVIRONMENT",
            application: "APP-NAME",
            version: "APP-VERSION",
            publicKey: "TOKEN",
            ignoreUrls: [],
            ignoreErrors: [],
            customDomainUrl: "",
            labels: ["String": Any],
            debug: false
        )
        self.coralogixRum = CoralogixRum(options: options)
    }

    var body: some Scene {
        WindowGroup {
            ContentView(coralogixRum: $coralogixRum)
        }
    }
}

Configuration options for the Coralogix exporter

The CoralogixExporterOptions struct provides configuration options for the Coralogix exporter.

PropertyTypeDescriptionRequired
userContextUserContext?Configuration for user context.No
debugBoolTurns on/off internal debug logging.No
ignoreUrls[String]?URLs that partially match any regex in ignoreUrls will not be traced.No
ignoreErrors[String]?A pattern for error messages which should not be sent to Coralogix.No
coralogixDomainCoralogixDomainCoralogix account https://coralogix.com/docs/coralogix-domain/Yes
publicKeyStringCoralogix token, publicly-visible public_key value. This is a required property.Yes
environmentStringSpecifies the environment, such as development, staging, or production.Yes
applicationStringName of the applicationYes
versionStringVersion of the applicationYes
customDomainUrlString?Ignores CoralogixDomain URL and routes all data calls to a specific URL.No
labels[String: Any]?Sets labels that are added to every Span.No

Integration functions

Use this set of public functions to interact with the Coralogix exporter. These functions enable developers to perform various actions such as setting user context, defining labels, reporting errors, logging messages with specified severity levels, and shutting down the exporter.

setUserContext

Sets the user context.

public func setUserContext(userContext: UserContext)

setLabels

Sets the labels.

public func setLabels(labels: [String: Any])

reportError

Reports an error to Coralogix.

public func reportError(exception: NSException)
public func reportError(error: NSError)
public func reportError(error: Error)
public func reportError(message: String, data: [String: Any]?)

log

Logs a message to Coralogix with a specified severity and optional data.

public func log(severity: CoralogixLogSeverity, message: String, data: [String: Any]?)

shutdown

Shuts down the Coralogix exporter and marks it as uninitialized.

public func shutdown()

Define different levels of severity for log messages

The CoralogixLogSeverity enum represents the different severity levels for logs in the Coralogix logging system.

CaseRaw ValueSeverity
debug1Debug-level
verbose2Verbose-level
info3Informational
warn4Warning-level
error5Error-level
critical6Critical-level

Example

coralogixRUM.logMessage(severity: .error, message: "An error occurred in the application.")

Method swizzling and SwiftUI modifiers

Dynamically alter the behavior of existing methods at runtime in Swift and Objective-C using these best practices:

  • Method Swizzling: A technique used in Objective-C and Swift to change the implementation of an existing selector at runtime, useful for injecting custom behavior without altering the original code.
  • SwiftUI Modifiers: A declarative and safe approach to modifying views through view modifiers, providing better readability and maintainability.

Using CXViewModifier and trackCXView method, you can add custom behavior to views in a SwiftUI-friendly way.

Example

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, World!")
            .trackCXView(name: "ContentView")
    }
}

Support

Need help?

Our world-class customer success team is available 24/7 to walk you through your setup and answer any questions that may come up.

Feel free to contact us via our in-app chat or by emailing support@coralogix.com.

On this page