SwiftUI Integration for iOS

This guide provides instructions on how to integrate Instabug's Bug Reporting, Crash Reporting, and App Performance Monitoring features in your SwiftUI application. Following these steps, you can capture and report bugs and crashes from your SwiftUI application.

📘

We currently only support one method to detect SwiftUI screen names.

Define Screen Names

To track and identify screens in your SwiftUI app, you‘ll need to define screen names using the InstabugTraceView function. The screen name will correspond to the current view in your app, and Instabug will use the latest UI component to update screens.

Approach 1: Wrapping Instabug on your views

Wrap Instabug around each view you have in your app by using the following:

var body: some View {
      InstabugTracedView(name: "View Name") {
        VStack {
          Text("Hello, World!")
      }
  }
}

Approach 2: Appending Instabug to your views

This is achieved by doing the following:

  1. Call an API on the view that you want to make the root view.
  2. Using the API below, it will create you a new view and wrap the user view with a traced view.

Example:

var body: some View {
    VStack {
        Text("Hello, World!")
    }
    .instabugTracedView(name: "View Name")
}

Private Views

You can easily mark any SwiftUI view that might contain sensitive information, like payment details, as private. Any private view will automatically appear with a black overlay covering it in any screenshot.

To make a view private, you can use one of the following methods:

  • Method 1:
InstabugPrivateView {
  Text("Hello, World") 
}
  • Method 2:
Text("Hello, World")
     .instabug_privateView()

User Interactions - Automatic Approach

To automatically capture user interactions in the SwiftUI screens, please follow the below steps

  1. Download the script file from here.

  2. Add the zip file to your App folder then unzip it.

  3. Open Xcode, after choosing your app click on “Edit Scheme“

  4. Click on Build → Pre-actions → “+” → New Run Script Action

  5. Choose your framework in “Provide build settings from“

  6. Write the command: $PROJECT_DIR/../InstabugSwiftUIIntegrator -d $PROJECT_DIR/YOUR-FRAMEWORK-FOLDER

  7. Click on Post-actions →”+” → New Run Script Action then choose your framework in “Provide build settings from“

  8. Write the command: $PROJECT_DIR/../InstabugSwiftUIIntegrator -r -d $PROJECT_DIR/YOUR-FRAMEWORK-FOLDER

  9. Click close then run.

The script will automatically capture the following:

  • Taps on the SwiftUI Screen
  • Any view that contains actions like Slider, Stepper, and Toggle

For other unsupported gestures, please use the manual approach to log them manually.

📘

Commands Configuration

  • --static or -s: The script should work on Instabug or InstabugStatic.
    • If you'll use -s (InstabugStatic), you should use the InstabugStatic custom build.
  • --directory or -d: Directory path to work on,
  • -bl or --black_list: Files names that shouldn’t be replaced. Matches substrings.
  • --revert or -r: reverts the changed code.

User Interactions - Manual Approach

To manually log user interactions on SwiftUI screens, you need to use the below API. The logged interactions will be reflected in the Repro Steps and User Steps components.

UserStep(event: .tap)  
            .setMessage("Hello, World")  
            .setViewType(.text)  
            .logUserStep()

Properties in the API:

  • Event: User action on the screen (Ex. Tap, scroll..)
  • Message: A brief message to help in identifying the UI element (Ex. Sign in)
  • View Type: the type of the Ui element (Ex. Button, dropdown, text..)