Swift components for embedding Mobilerun device streams in your iOS app.
Live WebRTC video, remote input and task approvals with customizable SwiftUI controls.
Building Mobilerun phones into your platform? Start with the Partner guide. It walks through the API key, one phone per user, stream keys and live viewing step by step.
macOS supports transport-independent tests only. MobilerunStream is the only library
product; the protocol target is an implementation detail.
Pass the device's backend-provided publishCapabilities in StreamConfig to enable the floating
microphone/camera pill. Use session.startPublish(PublishOptions(audio: true, video: true)),
await session.stopPublish() and session.$publishState for custom controls. Add the required iOS
privacy usage descriptions; capture begins only after the device accepts an explicit start. The server authorizes every
request. See publishing for setup, errors and lifecycle behavior.
Install version 0.1.0 with Swift Package Manager. Add it to your manifest’s dependencies:
.package(url: "https://github.com/droidrun/mobilerun-swift", exact: "0.1.0")Add the library product to your app target’s dependencies:
.product(name: "MobilerunStream", package: "mobilerun-swift")In Xcode, add https://github.com/droidrun/mobilerun-swift in Package
Dependencies, choose Exact Version 0.1.0, and select MobilerunStream.
Then import MobilerunStream.
Swift Package Manager resolves exact version 0.1.0 from the v0.1.0 release tag.
The tag must exist before installing this version.
See the connection guide for credentials and lifecycle setup.
- Your Node.js server mints a short-lived stream key for one device with
client.streamTokens.create; follow the Partner guide. - Your app's
tokenProviderasks your server for a key before each connection. - The SDK opens the stream with that key, one key per connection, and reconnects with a fresh one.
On your server, use @mobilerun/sdk 6.3.0 with MOBILERUN_API_KEY in the server environment:
npm install @mobilerun/[email protected]import { MobilerunSDK } from '@mobilerun/sdk';
const client = new MobilerunSDK({
apiKey: process.env.MOBILERUN_API_KEY,
logLevel: 'off',
});
// iOS: omit origins. For browsers, use a server-approved array of exact origins.
const allowedOrigins = undefined;
// Authenticate your user and authorize access to deviceId before minting.
const result = await client.streamTokens.create(
{ deviceId, ttlSeconds: 120, allowedOrigins },
{ maxRetries: 0 },
);
// result is { key, keyId, expiresAt }. Return result.key through your authenticated endpoint.
// Never log or cache the key. Keep MOBILERUN_API_KEY on the server.To revoke a token on the server, call await client.streamTokens.delete(result.keyId).
Raw REST fallback: POST /v1/identity/stream-keys accepts the same request fields.
In your app, supply your backend's stream URL and key calls:
import MobilerunStream
let session = DeviceStreamSession(credentials: {
guard let url = try await backend.streamURL(deviceID) else { return nil }
return StreamCredentials(url: url, tokenProvider: {
try await backend.mintStreamKey(deviceID)
})
})
session.start() // In the main-actor session owner.
DeviceStream(session: session) // In your SwiftUI view.Keep the session across view remounts and call session.close() on owner teardown.
Keep your API key on the server; recreate the session when switching devices.
A stream key works for one connection. The SDK calls tokenProvider before every connect
attempt, including each reconnect, and never reuses a key. If the provider throws or takes
longer than 10 seconds, the attempt fails with credentials_unavailable and the normal backoff applies.
tokenProvider is the recommended form. The static StreamCredentials(url:token:) form is
legacy and deprecated until the legacy token hard cut, and is an explicit exception to the
no-reuse rule: it keeps working and a reconnect reuses the same token. That only suits tokens
that stay valid; single-use stream keys need the provider. The SDK logs one warning per session
that uses a static token. If both are given, tokenProvider wins.
session.onError = { error in
// error.reason, error.message, error.attempt, error.final
}
// Or read session.lastError from SwiftUI; it clears once the stream connects.reason |
Meaning |
|---|---|
credentials_unavailable |
The provider threw, timed out or returned a blank key. |
connection_refused |
The socket closed or failed before opening: wrong device, refused origin (403), 404, network. |
token_rejected |
The upgrade answered 401: a used, expired or revoked key. |
disconnected |
An open session dropped, including the server's 30-minute session cap. |
device_unavailable |
The server explicitly reported the device offline or not ready; not inferred from timeouts or statuses. |
unknown |
Anything else, including a missing configuration after the socket opened. |
final is true when the SDK stops retrying; the default UI then shows its Retry button.
Every string the default UI renders comes from StreamLabels, with English defaults:
DeviceStream(session: session)
.streamLabels(StreamLabels(connecting: "Verbinde…", disconnected: "Getrennt",
unavailable: "Gerät nicht erreichbar", retry: "Erneut versuchen"))Coding agents: use the device stream integration skill.
| Guide | Covers |
|---|---|
| Stream keys and connection | Credentials, retries, timeouts, transport, audio and WebRTC compatibility. |
| Video and input | SwiftUI/UIKit views, touch, keyboard, clipboard and display-only mode. |
| Default UI | Themes, labels, slots, white-label controls and Liquid Glass. |
| Lifecycle | Visibility, foreground recovery and network transitions. |
| Task approvals | Polling, subscriptions, decisions, countdown and adaptive cards. |
| Statistics and benchmark | Sampling, measurements and the simulator benchmark. |
See CONTRIBUTING.md for builds, tests, conformance fixtures, the example app and releases. Report vulnerabilities privately using SECURITY.md.
Apache-2.0; see LICENSE. The LiveKit WebRTC binary has separate upstream licenses; see THIRD_PARTY_NOTICES.md.
