# Text consent for Swift

The SwiftUI opt-in step and consent box for your own iOS app, their parameters, the calls they make and what they record.

`ProvenanceTextConsent` puts the [consent to text](https://provenanceauth.com/docs/messaging/text-consent.md) step and box in your own iOS app, as two SwiftUI views: `TextConsentStep` before your request screen and `TextConsentView` on your sign-up or intake screen. Each records with `surface: "swift"`. Both need iOS 16 or macOS 13, and Foundation and SwiftUI only.

**This package isn't on the Swift Package Index yet.** Until then, every request in these docs runs over HTTP against the [sandbox](https://provenanceauth.com/docs/get-started/sandbox.md).

With access to the private Provenance repository, which your Provenance contact grants, install from it:

```bash
git clone https://github.com/michaelnbane-jpg/provenance.git provenance
// Package.swift: the package, then the product in your target
.package(path: "provenance/swift/provenance-text-consent")
.product(name: "ProvenanceTextConsent", package: "provenance-text-consent")
```

## TextConsentStep

The opt-in step a company's app shows before its own request screen, under **Provenance asks**. It reads the request, shows **Texts from** with your company's name and the masked phone, Provenance's wording with **Terms** · **Privacy**, **Not now** and **Text me**, **Next** with the request, and the Provenance mark.

```swift
import ProvenanceTextConsent

TextConsentStep(
    baseURL: apiURL,
    source: .captureEmbedToken(embedToken: captureToken, publishableKey: publishableKey)
) { outcome in
    showRequestScreen()
}
```

| Parameter | Type | What it is |
| --- | --- | --- |
| `baseURL` | `URL` | Your Provenance API's base URL |
| `source` | `TextConsentStepSource` | The request: `.captureEmbedToken(embedToken:publishableKey:)` or `.captureLink(token:)` |
| `transport` | `TextConsentTransport` | Optional. The HTTP client; `URLSessionTransport()` by default |
| `onError` | `(TextConsentError) -> Void` | Optional. A refusal or a network failure |
| `onFinish` | `(TextConsentStepOutcome) -> Void` | Called once when the step ends; show your request screen then |

Mint the embed token on your backend with `{ "widget": "capture", "object_id": "capr_…" }`, as for the [capture widget](https://provenanceauth.com/docs/embeds.md#2-mint-an-embed-token-on-your-backend). The capture link's token is the part after `/capture/` in the request's link.

| `TextConsentStepOutcome` | When |
| --- | --- |
| `.agreed` | **Text me** recorded the consent |
| `.notNow` | **Not now**: nothing was sent and nothing is stored |
| `.notOffered` | The request offers no step, or it couldn't be read: the view draws nothing and finishes at once |

The step is offered only when your mode is **Provenance asks**, you're on the Pro plan or above, the request carries a phone with `text` in its `channels`, and that phone has no consent for you and no stop. A refused **Text me** stays on the step with the reason under the wording.

## TextConsentView

The consent box for your own form, in either mode. One box, unticked on every load, with the wording beside it, **Terms** · **Privacy** and the Provenance mark. Ticking it with a valid phone records a `box` consent; unticking withdraws it. On the Free plan it draws nothing.

```swift
TextConsentView(
    baseURL: apiURL,
    publishableKey: publishableKey,
    embedToken: textConsentToken,
    phone: mobile
) { consentId in
    self.consentId = consentId
}
```

| Parameter | Type | What it is |
| --- | --- | --- |
| `baseURL` | `URL` | Your Provenance API's base URL |
| `publishableKey` | `String` | A publishable key with no allowed origins: an app sends no `Origin` |
| `embedToken` | `String` | A token your backend minted with `{ "widget": "text_consent", "object_id": "<box_id>" }` |
| `phone` | `String` | The mobile number your screen collected: E.164, or ten US digits |
| `onConsented` | `(String) -> Void` | Optional. The record's id after a tick |
| `onWithdrawn` | `(String) -> Void` | Optional. The record's id after an untick |
| `onError` | `(TextConsentError) -> Void` | Optional. A refusal or a network failure |

Read the record from your backend with [`GET /v1/account/text_consents/{id}`](https://provenanceauth.com/docs/api/account.md#get-v1-account-text-consents-id) before you rely on it. It shows as **Your app** in **Agreed to texts**, with no `form_origin`.

Neither view takes a colour, font or checked argument: the wording, the box and the mark can't be restyled or pre-ticked. Both follow the system's light or dark appearance.

## The calls they make

| View | Read | Record | Undo |
| --- | --- | --- | --- |
| `TextConsentStep`, embed token | `GET /v1/embed/capture_request` `?surface=swift` | `POST /v1/embed/text_consents` `{ step: true, wording_version, surface }` | None: a consent is withdrawn by the person |
| `TextConsentStep`, capture link | `GET /v1/attest/capture_links` `?surface=swift` | `POST /v1/public/text_consents` `{ link_token, wording_version, surface }` | None |
| `TextConsentView` | `GET /v1/embed/text_consents/form` `?surface=swift` | `POST /v1/embed/text_consents` `{ phone, wording_version, surface }` | `POST /v1/embed/text_consents/{id}/withdraw` |

The step never sends a phone: the token decides your account, the request and its phone. Every call sends `User-Agent: provenance-swift/1.0.0 (text-consent)`. `TextConsentClient` makes the same calls without a view, and `TextConsentStepModel` and `TextConsentBoxModel` hold the views' state for your own layout or tests.

## TextConsentError

| Field | What it is |
| --- | --- |
| `code` | The API's error code, such as `plan_required` or `parameter_invalid`, or `network_error` when nothing came back |
| `message` | A sentence you can show |
| `status` | The HTTP status, or `0` with no answer |

## See also

- [Get consent to text](https://provenanceauth.com/docs/messaging/text-consent.md): the modes, the step, the box and the records
- [Text consent for Kotlin](https://provenanceauth.com/docs/messaging/text-consent/kotlin.md): the same step and box in Jetpack Compose
- [Embeds](https://provenanceauth.com/docs/embeds.md): publishable keys and embed tokens
