# Flutter SDK

**Requirements:** Flutter 3.38+, Dart 3.10+, iOS 15+ and/or Android API 26+. The plugin runs on iOS and Android only. Calling it on another platform throws an `UnsupportedError`.

**Source:** [github.com/WarpLinkApp/warplink-flutter-sdk](https://github.com/WarpLinkApp/warplink-flutter-sdk)

## Installation

```bash
flutter pub add warplink_flutter
```

This adds the package to your `pubspec.yaml`:

```yaml
dependencies:
  warplink_flutter: ^1.4.0
```

The plugin wraps the native WarpLink iOS and Android SDKs, so link handling, deduplication, and attribution behave exactly as they do in a native app. The iOS SDK is distributed through Swift Package Manager. If your Flutter version does not use Swift Package Manager by default, turn it on once:

```bash
flutter config --enable-swift-package-manager
```

There is no Podfile change and no pod to install.

## Platform Setup

A Flutter app is one WarpLink app that covers both platforms. Register it once in the dashboard with iOS and Android selected, and do not create a second app for the other platform. Both platforms need the link domain configured. The plugin registers itself with Flutter's iOS and Android lifecycles, so you edit **no** `AppDelegate`, `SceneDelegate`, or `MainActivity`.

### iOS

1. Open `ios/Runner.xcworkspace` in Xcode, select the **Runner** target > **Signing & Capabilities**
2. Add **Associated Domains** > `applinks:yourapp.aplnk.to`
3. Enable Associated Domains in your App ID at [developer.apple.com](https://developer.apple.com)

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).

That is the whole iOS setup. The capability can also live in `ios/Runner/Runner.entitlements`.

### Android

Add the intent filter to your main Activity in `android/app/src/main/AndroidManifest.xml`:

```xml
<intent-filter android:autoVerify="true">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="https" android:host="yourapp.aplnk.to" />
</intent-filter>
```

Then set the minimum SDK to 26 in `android/app/build.gradle.kts`. The Flutter default can be lower, and the manifest merge then fails because the native WarpLink SDK needs API 26:

```kotlin
android {
    defaultConfig {
        minSdk = 26
    }
}
```

Keep the `android:launchMode` your Flutter project already has. The default Flutter template uses `singleTop`, which routes a link tapped on a running app to the existing Activity. `standard` and `singleInstance` are not supported.

Serving links from a [custom domain](https://warplink.app/docs/concepts/custom-domains)? Add it to the associated domains and the intent filter alongside `yourapp.aplnk.to`, then declare it in [`configure()`](#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` on iOS. 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).

### Flutter's Built-In Link Handling

Flutter has its own deep link handler, on by default. It forwards every incoming link to your router as a route, so `https://yourapp.aplnk.to/abc123` becomes a lookup for `/abc123` and can land on an unknown-route page. Switch it off and let `onLink` drive navigation.

In `ios/Runner/Info.plist`:

```xml
<key>FlutterDeepLinkingEnabled</key>
<false/>
```

In `android/app/src/main/AndroidManifest.xml`, inside your main `<activity>`:

```xml
<meta-data android:name="flutter_deeplinking_enabled" android:value="false" />
```

See [Routing with go_router](https://warplink.app/docs/sdks/flutter/deep-links#routing-with-go_router) for routing WarpLink links in your app.

## Configure the SDK

Call `WarpLink.configure()` once from your root widget's `initState`. Pass an `onLink` callback and the SDK wires cold start, warm start, and the deferred deep link check into that single sink.

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`.

```dart
import 'package:flutter/material.dart';
import 'package:warplink_flutter/warplink_flutter.dart';

void main() => runApp(const App());

class App extends StatefulWidget {
  const App({super.key});

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  @override
  void initState() {
    super.initState();
    WarpLink.configure(
      apiKey: 'wl_live_yoursdkkeyhere000000000000000000',
      debugLogging: true, // Enable native console logging
      onLink: (event) {
        switch (event) {
          case LinkEvent(:final deepLink):
            // Fired for taps AND deferred installs. Check deepLink.isDeferred.
            navigateTo(deepLink.deepLinkUrl ?? deepLink.destination);
          case ErrorEvent(:final error):
            debugPrint('WarpLink ${error.wireCode}: ${error.message}');
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(navigatorKey: navigatorKey, onGenerateRoute: buildRoutes);
  }
}
```

`navigateTo` waits for the navigator before it pushes a route, so a link that launches the app is never dropped. See [Deep Links](https://warplink.app/docs/sdks/flutter/deep-links#with-a-navigator-key) for `navigatorKey` and `navigateTo`.

`onLink` receives a `WarpLinkEvent`, which is either a `LinkEvent` or an `ErrorEvent`. A `switch` over the two is exhaustive.

`configure()` returns a `Future<void>` that completes when native configuration completes. A configuration failure is delivered to `onLink` as an `ErrorEvent` when you pass `onLink`, as in the example above. Only when you omit `onLink` does the returned future complete with a `WarpLinkException`, so `await` it in that case:

```dart
try {
  await WarpLink.configure(apiKey: 'wl_live_yoursdkkeyhere000000000000000000');
} on WarpLinkException catch (error) {
  // No onLink was passed, so a configuration failure surfaces here instead.
}
```

### Declare Your Link Domains

The SDK always recognizes `aplnk.to`. List your app's subdomain, `yourapp.aplnk.to`, and any custom domain you serve links from in `linkDomains`. `configure()` passes the list straight to the native iOS and Android SDKs, so a link on those hosts resolves on the very first launch, with no network round trip.

```dart
// Call from your root widget's initState
WarpLink.configure(
  apiKey: 'wl_live_yoursdkkeyhere000000000000000000',
  linkDomains: ['yourapp.aplnk.to', 'links.yourapp.com', 'go.yourapp.com'],
  onLink: (event) {
    if (event is LinkEvent) {
      navigateTo(event.deepLink.deepLinkUrl ?? event.deepLink.destination);
    }
  },
);
```

Declaring your domains lets the native side claim your links on the very first launch.

The native declaration works too, and is read before any Dart runs: a `WarpLinkDomains` array in `ios/Runner/Info.plist` on iOS, an `app.warplink.DOMAINS` meta-data entry in `AndroidManifest.xml` on Android. See the [iOS](https://warplink.app/docs/sdks/ios#declare-your-link-domains) and [Android](https://warplink.app/docs/sdks/android#declare-your-link-domains) guides.

Every form is optional and additive. Domains are normalized natively (trimmed, lowercased, a full URL reduced to its host), so paste whichever form you have.

### Opt Out of Automatic Handling

Both pieces are on by default. Disable either to drive it yourself with the manual methods:

```dart
WarpLink.configure(
  apiKey: 'wl_live_yoursdkkeyhere000000000000000000',
  automaticDeepLinks: false,          // use onDeepLink / getInitialDeepLink yourself
  automaticDeferredDeepLinks: false,  // use checkDeferredDeepLink yourself
);
```

`automaticDeepLinks` only takes effect when you pass `onLink`.

## Testing

### iOS: Physical Device Required

1. Build and run on a physical device with `flutter run -d <device-id>`
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 the Xcode console for `[WarpLink]` log messages

On the iOS Simulator, `xcrun simctl openurl booted https://yourapp.aplnk.to/abc123` opens the app when its Associated Domains entitlement lists the host and the domain serves a matching apple-app-site-association file. Test the iOS path on a physical device before you ship.

### Android: Physical Device or Emulator

```bash
# Verify App Links status
adb shell pm get-app-links com.yourcompany.yourapp

# Open a test link
adb shell am start -a android.intent.action.VIEW \
    -d "https://yourapp.aplnk.to/abc123" com.yourcompany.yourapp

# Filter Logcat for WarpLink messages
adb logcat -s WarpLink
```

Android App Links work on both a physical device and the emulator.

### Deferred Deep Links

Testing a deferred deep link, meaning a click that happens before the app is installed, needs a delete/uninstall-then-reinstall cycle so the SDK treats it as a fresh install. See [Testing](https://warplink.app/docs/sdks/flutter/deferred#testing) in the Deferred Deep Links guide.

## 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 by read mode. Tether needs SDK 1.4.0 or newer. Android needs no change, because the install referrer carries the link.

Set `tetherReadMode` to a `TetherReadMode` value. The default is `TetherReadMode.pasteButton`, and the other values are `automatic` and `onCall`.

```dart
await WarpLink.configure(
  apiKey: warpLinkKey,
  tetherReadMode: TetherReadMode.pasteButton,
);

// In the first-run screen. The system asks only after a tap.
WarpLinkPasteButton(onResult: (result) {
  if (result is TetherMatched) {
    // Continue to the screen the link points to.
  }
})
```

A read returns `TetherMatched`, `TetherNoCode`, `TetherUnusable` or `TetherTest`. After the SDK reads a test code, `await WarpLink.lastTetherTestReceipt()` returns 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.

```dart
const warpLinkKey = kDebugMode
    ? 'wl_test_yoursdkkeyhere000000000000000000'
    : 'wl_live_yoursdkkeyhere000000000000000000';
```

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 `WarpLinkDecodingException` 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/flutter/deep-links): Cold start, warm start, routing, and delivery limits
- [Deferred Deep Links](https://warplink.app/docs/sdks/flutter/deferred): Preserve context through installs
- [Attribution](https://warplink.app/docs/sdks/flutter/attribution): Platform-specific matching and privacy
- [Tether](https://warplink.app/docs/tether): Keep a link through the App Store on iPhone
- [Referral Invitations](https://warplink.app/docs/sdks/flutter/attribution#referral-invitations): Redeem a pasted invitation and credit a referral
- [API Reference](https://warplink.app/docs/sdks/flutter/api-reference): Full Dart API documentation
