iOS apps
This guide shows how to send signals from an iOS app with the Swift client.
You need the app's id and key. Make an app on the dashboard's Apps page at https://dashboard.omertasignals.com and copy both. The key is shown once: see Getting started.
Add the package
In Xcode, choose File > Add Package Dependencies and enter:
https://github.com/AlexandrosMichael/omerta-swift
Set the rule to "Up to Next Major Version" from 0.1.0, and add the product Omerta to your app's target.
Or in Package.swift:
.package(url: "https://github.com/AlexandrosMichael/omerta-swift", from: "0.1.0")
// then, in your target:
.product(name: "Omerta", package: "omerta-swift")
It needs iOS 15 or later and has no dependencies.
import Omerta
Create it
let omerta = try OmertaClient(
endpoint: "https://api.omertasignals.com",
app: "<the app's id>",
key: "<the app's key>"
)
endpointis where the service runs. It must be an http or https URL. A trailing/is ignored.appandkeyare the id and key from the Apps page.
The initializer throws an OmertaError if one of them is wrong.
The key ships inside the app and can be read from it. That is acceptable: it lets someone send signals for your app, not read them.
Which identifier
The package never reads an identifier by itself. Your app passes one to identify.
An identifier the app supplies, such as an account id:
omerta.identify(account.id)
Or the vendor identifier, which the app reads itself, on the main actor:
@MainActor
func identifyDevice(_ omerta: OmertaClient) {
omerta.identify(UIDevice.current.identifierForVendor?.uuidString)
}
What identifierForVendor means for the counts:
- It is the same for all of one vendor's apps on a device.
- It changes when the user removes every app of that vendor and installs again, so that person is counted as a new one.
- It can be
niljust after a restart, before the device is unlocked. Then later signals carry no user. Retry once the value is there.
Identify, track, flush
identify(_ user: String?) says who the following signals belong to. Call identify(nil) on sign-out, so later signals carry no user. A signal keeps the user that was set when track was called.
omerta.identify("ada@example.com")
try omerta.track("app.launched")
try omerta.track("plan.chosen", ["plan": "pro"])
omerta.identify(nil)
track(_ type: String, _ payload: [String: String]? = nil) queues a signal, stamped with the current time in whole seconds. It throws an OmertaError at once if the input is wrong, and queues nothing:
typemust be 1 to 64 characters, start with a letter, and hold only letters, digits,.,_and-.payloadis optional, with at most 32 keys.- Each payload key must be 1 to 64 characters.
- Each payload value must be at most 1000 characters. Characters are counted as UTF-16 code units, as the server does.
flush() is async. It sends what is queued and returns the number of signals the server accepted. It sends at most 1000 signals and 1,048,576 bytes per request, in queue order. Calls never overlap: a later call waits for the running one.
do {
let accepted = try await omerta.flush()
} catch let error as OmertaError {
print(error.message) // the server's error, or "HTTP <status>"
}
If the server refuses a batch with 400, those signals are dropped from the queue and flush() throws an OmertaError with the server's error as message. Any other failure keeps the signals queued, so the next flush() sends them again. A network failure is thrown as the error URLSession raised, not as an OmertaError.
The queue holds at most 10000 signals. When track is called on a full queue, the oldest is dropped, and nothing is thrown.
When to flush
The client has no timers, so the app decides when to flush. This SwiftUI view flushes when the scene enters the background, and every 30 seconds while it is active:
struct RootView: View {
let omerta: OmertaClient
@Environment(\.scenePhase) private var scenePhase
var body: some View {
ContentView()
.onChange(of: scenePhase) { phase in
if phase == .background {
Task { _ = try? await omerta.flush() }
}
}
.task(id: scenePhase) {
guard scenePhase == .active else { return }
while !Task.isCancelled {
try? await Task.sleep(nanoseconds: 30_000_000_000)
_ = try? await omerta.flush()
}
}
}
}
Signals still queued when the app is killed are lost, because the client stores nothing on disk.
What leaves the device
The identifier you pass to identify is hashed on the device. The client sends a SHA-256 hash of it together with the app id, never the identifier itself. The server hashes that value again with a key it keeps, and stores only the result. See How identifiers are kept.
The stored data is pseudonymous: with the server's secret, a known identifier can be matched to its signals.
With an AI assistant
To set this up with an AI assistant, see the agents page (/agents).