> ## Documentation Index
> Fetch the complete documentation index at: https://docs.partnero.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Swift SDK

> Link iOS and macOS app users to the partner who referred them, from a typed code or a partner app link.

Use the Partnero Swift package to tell Partnero which partner referred an app user. After that, any purchase recorded for the user, through [RevenueCat](https://docs.partnero.com/integrations/revenuecat.md) or your backend, earns that partner a commission. The [Mobile apps](https://docs.partnero.com/guides/tracking/mobile-app.md) guide explains the whole flow.

* iOS 15+ and macOS 12+, Swift 6, Xcode 16+
* No dependencies, no analytics and no background work
* Uses only your **Program ID**, never your API key

Source: [github.com/partnero/partnero-swift](https://github.com/partnero/partnero-swift)

## Before you start

In Partnero, open **Program settings** and turn on **Enable mobile app claims** in the **Tracking for mobile apps** section. Until then, every claim fails with `codeNotFound`.

Copy your **Program ID** from the program's **Integration** page. It's the same ID as in your PartneroJS snippet.

## Install

In Xcode, choose **File** → **Add Package Dependencies** and enter `https://github.com/partnero/partnero-swift`. Or add it to `Package.swift`:

```swift theme={"system"}
.package(
    url: "https://github.com/partnero/partnero-swift",
    from: "0.1.0"
)
```

## Configure

Call `configure` once at launch, before any claim:

```swift theme={"system"}
import Partnero

@main
struct MyApp: App {
    init() {
        Partnero.configure(programId: "YOUR_PROGRAM_ID")
    }

    var body: some Scene {
        WindowGroup { ContentView() }
    }
}
```

The first call records when the app was first opened on this device, which the program's claim window counts from.

Referral links use `?aff=` by default. If yours use another parameter, such as `?ref=DANIEL20`, pass `referralParameter: "ref"`.

## Claim a code the user enters

Claim the code when the user enters one. Pass the ID your purchases are reported under, such as the RevenueCat app user ID:

```swift theme={"system"}
do {
    let result = try await Partnero.claim(
        code: "DANIEL20",
        appUserId: Purchases.shared.appUserID
    )

    switch result.status {
    case .claimed: break        // attributed to the code's partner
    case .alreadyClaimed: break // was attributed already; nothing changed
    }
} catch PartneroError.codeNotFound {
    // show "That code isn't valid"
} catch {
    // network or server error; let the user try again
}
```

Each user can be linked to one partner only, so the first successful claim is final. With RevenueCat, claim after `Purchases.shared.logIn(...)`, since anonymous IDs are rejected.

If a code is used as both a partner's link key and a promotion code, say which one the user entered:

```swift theme={"system"}
try await Partnero.claim(code: "DANIEL20", appUserId: user.id, codeType: .promotionCode)
```

Save the code on the RevenueCat customer as well. Then a purchase can still be credited to the partner if the claim request was lost:

```swift theme={"system"}
Purchases.shared.attribution.setAttributes(
    Partnero.revenueCatAttributes(code: "DANIEL20")
)
```

## Claim from an opened link

When a [partner app link](https://docs.partnero.com/guides/tracking/mobile-app.md#partner-app-links) or a referral link opens your app, pass the URL on:

```swift theme={"system"}
.onOpenURL { url in
    Task {
        _ = try await Partnero.claim(
            url: url,
            appUserId: Purchases.shared.appUserID
        )
    }
}
```

The code is read from `partnero_code`, or from your referral parameter, such as `https://example.com/?aff=DANIEL20`. The click ID is read from `partnero_click`. It returns `nil` without calling Partnero for links with neither.

If the user isn't signed in yet, store the values and claim after sign-in:

```swift theme={"system"}
let code = try Partnero.code(from: url)
let clickId = try Partnero.clickId(from: url)

// after sign-in
if let code {
    try await Partnero.claim(code: code, appUserId: user.id, clickId: clickId)
} else if let clickId {
    try await Partnero.claim(clickId: clickId, appUserId: user.id)
}
```

With a deep linking provider such as AppsFlyer, Branch or Adjust, read the click from its callback and call `Partnero.claim(clickId:appUserId:)`.

## Claim results and failures

`claim` returns a `ClaimResult` with a `status`:

| `status` | What it means |
| - | - |
| `.claimed` | Partnero linked the user to the partner and created the customer |
| `.alreadyClaimed` | The user was linked to a partner before, and that link stays as it was |

It throws a `PartneroError`:

| Error | When |
| - | - |
| `.notConfigured` | `configure` wasn't called |
| `.invalidInput(field:)` | A blank or malformed code, click ID, app user ID or Program ID |
| `.codeNotFound` | No active code or click matches, or mobile app claims are off |
| `.codeConflict(message:)` | The code matches more than one partner, or the claim window has closed |
| `.rateLimited` | More than 30 claims a minute from one network. Try again in a minute |
| `.network(message:)` | The request didn't reach Partnero |
| `.server(status:message:)` | Any other response |

The SDK never retries on its own. Your app decides whether and when to try again.

## Privacy

The SDK sends Partnero the code, the click ID when there is one, the app user ID, the platform and the first launch date. It stores only the first launch date, in `UserDefaults`, and ships a privacy manifest. List the user ID under **App Functionality** in your App Store privacy details if you don't already.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.