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 SwiftUI Screens
In order to track and identify screens in your SwiftUI app, you‘ll need to use the InstabugTraceView
function.
This will allow the SDK to measure the loading time of your SwiftUI views and allow you to define screen names for your SwiftUI views. The screen name will correspond to the current view in your app, and Instabug will use the latest UI component to update screens.
Only the wrapper approach (Approach 1) Will allow the SDK to measure Screen Loading time of your SwiftUI Views
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 Modifier to your views
If you don’t use App Performance Monitoring, or don’t want to track your screen loading times, you can also define screen names by doing the following:
- Call an API on the view that you want to make the root view.
- 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
-
Download the script file from here.
-
Add the zip file to your App folder then unzip it.
-
Open Xcode, after choosing your app click on “Edit Scheme“
-
Click on Build → Pre-actions → “+” → New Run Script Action
-
Choose your framework in “Provide build settings from“
-
Write the command:
$PROJECT_DIR/../InstabugSwiftUIIntegrator -d $PROJECT_DIR/YOUR-FRAMEWORK-FOLDER
-
Click on Post-actions →”+” → New Run Script Action then choose your framework in “Provide build settings from“
-
Write the command:
$PROJECT_DIR/../InstabugSwiftUIIntegrator -r -d $PROJECT_DIR/YOUR-FRAMEWORK-FOLDER
-
Click close then run.
The script will automatically capture the following:
- Taps on the
SwiftUI
Screen - Any view that contains actions like
Slider
,Stepper
, andToggle
For other unsupported gestures, please use the manual approach to log them manually.
Commands Configuration
--static
or-s
: The script should work onInstabug
orInstabugStatic
.
- If you'll use
-s (InstabugStatic)
, you should use theInstabugStatic
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..)
Updated about 1 month ago