# iOS SDK

**Requirements:** iOS 15+, Swift 5.9+, Xcode 15+. Confirm Universal Links on a physical device: the Simulator opens them only through `xcrun simctl openurl`.

## Installation

1. Go to **File > Add Package Dependencies...**
2. Enter: `https://github.com/WarpLinkApp/warplink-ios-sdk`
3. Select **Up to Next Major Version** and click **Add Package**

```swift
dependencies: [
    .package(url: "https://github.com/WarpLinkApp/warplink-ios-sdk", from: "1.4.1")
]
```

## Configure Associated Domains for Universal Links

1. In Xcode, select your app target > **Signing & Capabilities**
2. Click **+ Capability** > **Associated Domains**
3. Add: `applinks:yourapp.aplnk.to` (find your app's subdomain in the dashboard: open the app, then **Settings**, or see [Your app's own link address](https://warplink.app/docs/migration/aasa-subdomain))

Also enable Associated Domains in your App ID at [developer.apple.com](https://developer.apple.com).

Serving links from a [custom domain](https://warplink.app/docs/concepts/custom-domains)? Add an entry for it too (`applinks:links.yourapp.com`), then [declare it to the SDK](#declare-your-link-domains).

Every app has its own subdomain, `yourapp.aplnk.to`. Add that exact host, never a wildcard such as `applinks:*.aplnk.to`. If your app existed before subdomains, keep `aplnk.to` listed next to the new address: links you already have stay on `aplnk.to` and keep opening your app. See [Your app's own link address](https://warplink.app/docs/migration/aasa-subdomain).

## Initialize the SDK

Call `configure()` as early as possible in your app lifecycle. Pass an `onLink` callback and the SDK wires up cold start, warm start, and the deferred deep link check automatically. Your one callback receives every resolved link.

The credential below is an **SDK key**, created in the dashboard under **Keys** > **SDK keys**. Configure the SDK with an SDK key, since only an SDK key records installs. If your organization has several apps, create the key for this app: a key tied to one app is refused for any other app with `KEY_APP_MISMATCH`.

```swift
import SwiftUI
import WarpLink

@main
struct MyApp: App {
    init() {
        WarpLink.configure(
            apiKey: "wl_live_yoursdkkeyhere000000000000000000",
            options: WarpLinkOptions(onLink: { result in
                if case .success(let deepLink) = result, let deepLink {
                    // Fired for taps AND deferred installs. Check deepLink.isDeferred.
                    // navigate(to:) stands for your own navigation code.
                    navigate(to: deepLink)
                }
            })
        )
    }

    var body: some Scene {
        WindowGroup {
            ContentView()
                .onOpenURL { WarpLink.open($0) }
        }
    }
}
```

```swift
import UIKit
import WarpLink

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        WarpLink.configure(
            apiKey: "wl_live_yoursdkkeyhere000000000000000000",
            options: WarpLinkOptions(onLink: { result in
                if case .success(let deepLink) = result, let deepLink {
                    navigate(to: deepLink)
                }
            })
        )
        return true
    }
}
```

Your `onLink` callback only fires for a real resolved link or an error. A deferred check that finds no match is not delivered to `onLink`, so the automatic sink stays quiet when there is nothing to route. Everything is dispatched on the main thread.

**From SDK 1.4.0,** the `onLink` parameter of `WarpLinkOptions` is `@MainActor`. Your handler can call your own main-actor code, such as a navigation function, and Swift 6 or Xcode 26 default isolation compiles it without `MainActor.assumeIsolated`. The `WarpLinkOptions.onLink` property keeps its `@Sendable` type, so code that reads it and calls it compiles as before.

### Deliver Incoming URLs

The SDK still needs the tapped URL from the OS. Pick one of these (no swizzling):

- **SwiftUI app:** use `.onOpenURL { WarpLink.open($0) }` on your root view, as in the sample above. The delegate classes below are for UIKit apps.
- **Drop-in delegate (UIKit):** set your scene delegate class to `WarpLinkSceneDelegate` (or app delegate to `WarpLinkAppDelegate`) and the SDK forwards Universal Links for you.
- **One-liner (UIKit):** if you keep your own delegate, forward the activity or URL:

```swift
// In your scene/app delegate
func scene(
    _ scene: UIScene,
    willConnectTo session: UISceneSession,
    options connectionOptions: UIScene.ConnectionOptions
) {
    connectionOptions.userActivities.forEach { WarpLink.continue($0) }
    connectionOptions.urlContexts.forEach { WarpLink.open($0.url) }
}

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    WarpLink.continue(userActivity)
}

func scene(_ scene: UIScene, openURLContexts contexts: Set<UIOpenURLContext>) {
    contexts.forEach { WarpLink.open($0.url) }
}
```

That is the entire host integration. Resolution and the `onLink` dispatch are automatic.

### Opt Out of Automatic Handling

Every piece is on by default. Disable what you want to drive yourself:

```swift
WarpLink.configure(
    apiKey: "wl_live_yoursdkkeyhere000000000000000000",
    options: WarpLinkOptions(
        debugLogging: true,          // [WarpLink] console logs
        autoDeepLinkHandling: false, // you call WarpLink.handleDeepLink yourself
        autoDeferredCheck: false     // you call WarpLink.checkDeferredDeepLink yourself
    )
)
```

## Declare Your Link Domains

The SDK always recognizes `aplnk.to`. Tell it about your app's own subdomain, `yourapp.aplnk.to`, and any custom domain you serve links from. Then a link on those hosts resolves on the very first launch, with no network round trip.

This matters because the decision is synchronous. `WarpLink.open(_:)` returns a `Bool` the instant the OS hands over a URL, so it can only answer from what it already knows. A domain the SDK has not been told about is reported as not ours and handed back to your router, which is exactly what a fresh install looks like before the SDK has ever reached the server.

Declare them in code:

```swift
WarpLink.configure(
    apiKey: "wl_live_yoursdkkeyhere000000000000000000",
    options: WarpLinkOptions(
        linkDomains: ["yourapp.aplnk.to", "links.yourapp.com", "go.yourapp.com"],
        onLink: { result in
            if case .success(let deepLink) = result, let deepLink {
                navigate(to: deepLink)
            }
        }
    )
)
```

Or declare them with no code at all, in `Info.plist`:

```xml
<key>WarpLinkDomains</key>
<array>
    <string>yourapp.aplnk.to</string>
    <string>links.yourapp.com</string>
    <string>go.yourapp.com</string>
</array>
```

Both forms are optional and both are additive. The SDK uses the union of `aplnk.to`, `linkDomains`, the `WarpLinkDomains` array, and the domains it learns from the server. Declare your app's subdomain so a link on it resolves on a fresh install.

Entries are normalized for you: whitespace is trimmed, the value is lowercased, and a full URL is reduced to its host, so `https://Links.YourApp.com/` and `links.yourapp.com` mean the same thing. `www.` is left alone, because `www.yourapp.com` and `yourapp.com` are different hosts.

## Handle Deep Links

Cold and warm start are handled for you once URLs reach the SDK. See [Deep Links](https://warplink.app/docs/sdks/ios/deep-links) for the delegate options and the manual path.

## Handle Deferred Deep Links

The deferred check fires automatically from `configure()` and delivers its result to `onLink`. If you call `configure()` again with the same key and options, the SDK keeps the running session and its check, and only takes the new `onLink`. A different key or different options start a new session. See [Deferred Deep Links](https://warplink.app/docs/sdks/ios/deferred) for details and the manual path.

## Test Universal Links on a Physical Device

1. Build and run on a physical device
2. Paste your test link (e.g., `https://yourapp.aplnk.to/abc123`) into Notes or Messages and tap it. Typing it into the Safari address bar never opens the app.
3. The app should open and trigger the deep link callback
4. Check Xcode console for `[WarpLink]` log messages

On the iOS Simulator, `xcrun simctl openurl booted https://yourapp.aplnk.to/abc123` opens the app only when its Associated Domains entitlement lists the host and the domain serves a valid apple-app-site-association file that matches the path, and because tapping a link inside the Simulator is unreliable, confirm the iOS path on a physical device before you ship.

## Keep a link through the App Store with Tether

Tether carries a link through the App Store install on iPhone. The page the person tapped copies a short code, and the SDK reads it from the clipboard by read mode. Tether needs SDK 1.4.0 or newer. Route the link in `onLink` too: in the default mode it first gets an estimate, and the exact link can follow.

Set the read mode in the options. The default is `.pasteButton`, and the other modes are `.automatic` and `.onCall`.

```swift
WarpLink.configure(
    apiKey: warpLinkKey,
    options: WarpLinkOptions(tetherReadMode: .pasteButton)
)

// In the first-run screen. The system asks only after a tap.
// The paste button needs iOS 16; on iOS 15 use a plain button that calls readTether.
func handle(_ result: Result<TetherReadResult, WarpLinkError>) {
    // Continue to the screen the link points to when the result is a match.
}

if #available(iOS 16.0, *) {
    WarpLinkPasteButton(onTether: handle)
} else {
    Button("Paste your code") { WarpLink.readTether(completion: handle) }
}
```

The paste button needs iOS 16; on iOS 15 use a plain button that calls `WarpLink.readTether(completion:)`, as the snippet does. In `.onCall` mode, call `WarpLink.readTether(completion:)`. It returns a `TetherReadResult`: `.matched`, `.noCode(reason)`, `.unusable` or `.test(receipt:)`. After the SDK reads a test code, `WarpLink.lastTetherTestReceipt` holds the receipt. Pass it to `verify_setup` as `tether_test_receipt`. See [Tether](https://warplink.app/docs/tether/sdk-modes) for the modes, the [privacy page](https://warplink.app/docs/tether/privacy) for the App Review note, and [Test Tether](https://warplink.app/docs/tether/testing) for the check.

## Use a Test key in debug builds

Use a `wl_test_` key in debug builds and a `wl_live_` key in release builds. A Test key reads and writes [Test mode](https://warplink.app/docs/concepts/test-mode) data only, and the SDK keeps its stored attribution apart from live state on the same device.

```swift
#if DEBUG
let warpLinkKey = "wl_test_yoursdkkeyhere000000000000000000"
#else
let warpLinkKey = "wl_live_yoursdkkeyhere000000000000000000"
#endif
```

A Test key needs a Test app with its own bundle ID. Give the debug build that bundle ID, and add `applinks:<handle>-test.aplnk.to` to its Associated Domains, where `<handle>` is the handle of your live app. Without them, a Test link does not open the debug build. See [Test your integration](https://warplink.app/docs/guides/testing).

To test a first install again without deleting the app, call `WarpLink.clearAttribution()`. It clears the attribution stored on the device and starts a new check. With a `wl_live_` key it does nothing and succeeds. If a stored item cannot be deleted, it fails with `.decodingError` and the message "Could not clear stored attribution data." Calling it again is safe. See [Test your integration](https://warplink.app/docs/guides/testing).

## Next Steps

- [Deep Links](https://warplink.app/docs/sdks/ios/deep-links): Handle Universal Links for cold and warm start
- [Deferred Deep Links](https://warplink.app/docs/sdks/ios/deferred): Preserve context through App Store installs
- [Attribution](https://warplink.app/docs/sdks/ios/attribution): Understand match types and confidence scores
- [Tether](https://warplink.app/docs/tether): Keep a link through the App Store on iPhone
- [Referral Invitations](https://warplink.app/docs/sdks/ios/attribution#referral-invitations): Redeem a pasted invitation and credit a referral
- [API Reference](https://warplink.app/docs/sdks/ios/api-reference): Full Swift API documentation
