Swift SDK
Integrate the FlashAnalytics Swift SDK in native iOS apps.
Use the Swift SDK for native iOS apps with SwiftUI or UIKit. It supports product events, identity, revenue tracking, experiments, app lifecycle, deep links, screen tracking, view interactions, native crash capture, caught error reporting, and notification lifecycle helpers.
Install
.package(
url: "https://github.com/NextGenCreativeSolutions/flashanalytics-swift.git",
from: "1.1.0"
)The package currently targets iOS 15+ and macOS 12+.
Quick start
import FlashAnalytics
let analytics = FlashAnalytics.configureShared(
options: FlashAnalyticsOptions(
appId: "YOUR_APP_ID",
secretKey: "YOUR_SECRET_KEY",
endpoint: "https://api.flashanalytics.app",
captureAppLifecycle: true,
captureDeepLinks: true,
captureScreenViews: true,
captureViewInteractions: true,
captureNativeCrashes: true,
captureVariants: CaptureVariantsOptions(
onAssignmentsChanged: { assignments in
// called whenever the local cache changes
}
)
)
)
analytics.identify(
IdentifyPayload(
profileId: "user-123",
email: "user@example.com"
)
)
analytics.track("signup_clicked", properties: ["plan": "pro"])Auto capture
- App lifecycle via
captureAppLifecycle - UIKit screen views via
captureScreenViews - SwiftUI screen views via
flashAnalyticsScreen - Deep links via
handleOpenURLandhandleUserActivity - Native crashes via
captureNativeCrashesorcaptureErrors - Experiment auto-assignment via
captureVariants
Experiments
Enable captureVariants to keep a local cache of assignments in sync automatically. On each new session all experiments are fetched; after identify() only profile-mode assignments are refreshed; on session expiry only session-mode entries are removed.
// All cached assignments — no API call
let all = analytics.getAllExperiments()
// Single experiment — checks cache first, falls back to API if not found
let assignment = await analytics.getExperimentById(experimentId: "checkout-cta")
print(assignment?.variantName ?? "nil")
// Manual assignment (always calls the API)
let variant = await analytics.assignExperiment("checkout-cta")
let assignments = await analytics.autoAssignExperiments()Manual error tracking
do {
try riskyOperation()
} catch {
analytics.trackError(
error,
eventName: "payment_failed",
properties: ["orderId": orderId]
)
}
analytics.trackError("Unexpected nil value in user profile")SwiftUI
SomeView()
.flashAnalyticsScreen(analytics, path: "Home")
.onOpenURL { url in
analytics.handleOpenURL(url)
}Notification lifecycle
The Swift SDK supports delivered, opened, dismissed, action-clicked, and expired events. Delivery and expiry require a Notification Service Extension. Open, dismiss, and action events require app-side UNUserNotificationCenterDelegate integration.
let analytics = FlashAnalytics.configureShared(
options: FlashAnalyticsOptions(
appId: "YOUR_APP_ID",
endpoint: "https://api.flashanalytics.app",
capturePushLifecycle: true
)
){
"aps": {
"mutable-content": 1
}
}Session access
if let session = analytics.getSession() {
print(session.id)
print(session.estimatedExpiresAt)
print(session.estimatedTtlMs)
}