Mobile SDKsiOS (Swift)

iOS SDK (Swift)

The Datafly Signal iOS SDK is a native Swift package that captures events on-device and delivers them to your Signal pipeline. It has zero external dependencies and uses only Apple framework APIs.

Requirements

  • iOS 15.0+, tvOS 15.0+, or macOS 12.0+
  • Swift 5.9+
  • Xcode 15+

Installation

Add the Datafly Signal package to your Xcode project:

  1. In Xcode, go to File > Add Package Dependencies
  2. Enter the repository URL:
    https://github.com/wearedatafly/datafly-signal-ios
  3. Select the DataflySignal library and add it to your target

Or add it to your Package.swift:

dependencies: [
    .package(url: "https://github.com/wearedatafly/datafly-signal-ios", from: "1.0.0")
]

CocoaPods

pod 'DataflySignal', '~> 1.0'

Initialisation

Initialise the SDK as early as possible in your app lifecycle, typically in your AppDelegate or @main App struct:

import DataflySignal
 
@main
struct MyApp: App {
    init() {
        let config = DataflyConfig(
            pipelineKey: "dk_live_abc123",
            endpoint: "https://data.example.com"
        )
        Datafly.shared.initialize(config: config)
    }
 
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

UIKit

import DataflySignal
 
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        let config = DataflyConfig(
            pipelineKey: "dk_live_abc123",
            endpoint: "https://data.example.com"
        )
        Datafly.shared.initialize(config: config)
        return true
    }
}

Quick start

// Track a screen view
Datafly.shared.screen("Home")
 
// Track a custom event
Datafly.shared.track("add_to_cart", properties: [
    "item_id": "SKU-001",
    "item_name": "Running Shoes",
    "price": 79.99,
    "currency": "GBP"
])
 
// Identify a user after login
Datafly.shared.identify(userId: "user-123", traits: [
    "email": "[email protected]",
    "name": "Jane Doe",
    "plan": "professional"
])
 
// Associate with a company
Datafly.shared.group(groupId: "company-456", traits: [
    "name": "Acme Corp",
    "plan": "enterprise"
])
 
// Handle a deep link (extracts UTM params and click IDs)
Datafly.shared.handleDeepLink(url: incomingURL)
 
// Set IDFA after ATT consent
import AppTrackingTransparency
 
ATTrackingManager.requestTrackingAuthorization { status in
    if status == .authorized {
        let idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString
        Datafly.shared.setAdvertisingId(idfa, enabled: true)
    }
}
 
// Force flush (e.g., before app termination)
Datafly.shared.flush()
 
// Logout -- generates new anonymous ID
Datafly.shared.reset()

Device identifiers

The iOS SDK automatically collects:

IdentifierSourcePersistencePermission required
Anonymous IDUUID v4, SDK-generatedKeychain (survives reinstall)None
IDFVUIDevice.identifierForVendorSystem-managedNone
Device modelhw.machine via sysctlbynameN/ANone
IDFASet via setAdvertisingId()MemoryATT consent

The anonymous ID is stored in the iOS Keychain with kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly accessibility. This means it persists across app reinstalls and device restarts, providing stronger identity continuity than UserDefaults alone.

App lifecycle events

When trackAppLifecycle is enabled (default), the SDK automatically tracks:

EventWhenProperties
Application InstalledFirst launch after installversion, build
Application UpdatedFirst launch after app updateversion, build, previous_version, previous_build
Application OpenedApp enters foregroundversion, build, from_background
Application BackgroundedApp enters background

Configuration reference

PropertyTypeDefaultDescription
pipelineKeyStringRequiredYour source pipeline key
endpointStringRequiredYour Ingestion Gateway URL
flushIntervalTimeInterval30Seconds between automatic flushes
flushThresholdInt20Events queued before automatic flush
maxQueueSizeInt1000Maximum events stored locally
sessionTimeoutTimeInterval1800Seconds of inactivity before new session (30 min)
trackAppLifecycleBooltrueAutomatically track install/update/open/background
debugBoolfalseEnable debug logging to console

Debug mode

Enable debug logging to see all SDK activity in the Xcode console:

let config = DataflyConfig(
    pipelineKey: "dk_live_abc123",
    endpoint: "https://data.example.com",
    debug: true
)

You can also attach an event inspector callback:

Datafly.shared.onEvent = { eventJSON in
    print("Event queued: \(eventJSON)")
}

Next steps