Guide
How to add an in-app survey to a SwiftUI app
You can build a rating view in SwiftUI in an afternoon. Deciding who sees it, when, how often, and collecting the answers is the real work. Here's how to add surveys that handle all of that.
Add the MicroSurveys package with Swift Package Manager, create one SDK instance when your SwiftUI App starts, and forward events to it, either through the Amplitude plugin or with track() calls from your views. When an event matches a published survey, the SDK presents it natively over whatever's on screen, so you don't add a sheet or an overlay to your view hierarchy. Questions, triggers and styling live in the console and change without an App Store release.
Build it yourself or use an SDK?
A one-off rating view in SwiftUI is easy. A survey system is not, because most of the work isn't the view:
- Timing: showing it after the right event, maybe the third time, maybe after a delay.
- Fairness: sampling a percentage of users, targeting by plan or locale, and capping how often anyone is asked, consistently across launches.
- Reliability: working offline, queueing answers, never asking twice by mistake.
- Change: editing the question or pausing the survey without shipping a new build and waiting for review.
If you need one question once, build it. If you'll ask different questions at different moments, an SDK saves that work and keeps it out of your codebase.
1. Add the package
In Xcode, choose File → Add Package Dependencies…, paste the repository URL and add MicroSurveysSDK to your app target. Or in Package.swift:
.package(url: "https://github.com/MicroSurveysAI/microsurveys-ios", from: "0.0.8")It needs iOS 15 or later. Surveys are native UIKit views (no web views), presented over your SwiftUI hierarchy.
2. Start it in your App
Create one instance for the app's lifetime and start it when the app launches. If you use Amplitude, register the plugin so every event you track can trigger a survey:
import SwiftUI
import MicroSurveysSDK
import AmplitudeSwift
enum Surveys {
static let shared = MicroSurveysSDK(apiKey: "ms_live_xxx")
}
@main
struct JuniperApp: App {
init() {
// Forward Amplitude events (unchanged) and identity
amplitude.add(plugin: Surveys.shared.amplitudePlugin())
// Load cached surveys, then fetch the latest
Surveys.shared.start()
}
var body: some Scene {
WindowGroup { ContentView() }
}
}The SDK also refreshes its surveys when the app comes back to the foreground, at most every 12 hours.
3. Send events from your views
Without Amplitude (or for moments it doesn't track), call track() where the moment happens. In SwiftUI that's usually a button action or .onAppear:
struct OrderConfirmationView: View {
let order: Order
@Environment(\.dismiss) private var dismiss
var body: some View {
VStack {
Text("Your plants are on their way")
Button("Done") { dismiss() }
}
.onAppear {
Surveys.shared.track("checkout_completed", properties: ["items": order.items.count])
}
}
}Set who's answering with setUser(id:properties:) when they sign in, so targeting and frequency caps follow the person. With Amplitude, identity comes from Amplitude automatically.
4. Create and publish the survey
In the console, add a question (NPS, CSAT, CES, stars, thumbs, a choice or open text), set the trigger to checkout_completed, and choose a delay so the survey appears a couple of seconds after the confirmation, not on top of it. Add a sample percentage and a frequency cap, then publish. Devices pick it up on their next refresh.
5. Match your app's look
The survey takes its colors, corner radius and fonts from the theme you set in the console, and follows light and dark mode, so restyling never needs a release. It presents as a bottom sheet or a centered dialog. For an exact brand font, set it in code with SurveyTheme.
The SDK presents on the top-most view controller of the active scene, which is what SwiftUI apps want. If you need a specific anchor, set presentationAnchor. While developing, turn on MicroSurveysSDK.loggingEnabled and filter the Xcode console by [MicroSurveys] to see why a survey did or didn't show. Full reference: iOS SDK docs.
Questions
Both. The survey UI is built in UIKit and presented over your app, so it works the same whether your screens are SwiftUI, UIKit or a mix. You don't add any view to your SwiftUI hierarchy.
iOS 15 and later. The self-sizing sheet uses iOS 16 APIs and falls back gracefully on iOS 15.
It's distributed with Swift Package Manager, which Xcode supports natively.
No network call blocks launch: the SDK loads cached surveys from disk and refreshes in the background, and triggers are checked on the device.
Ask your first question this week.
Free for 1,000 responses a month per project. No card, no demo call.
