# Swift

MuxForms on Swift uses URLSession.shared.data(for:) with a POST request to the submit API, JSONEncoder for the body, and Authorization Bearer ingest secret. Handle HTTP 422 to show validation errors.

## Swift

| API | Usage |
| --- | --- |
| URLSession | POST JSON |
| Codable | Payload struct |
| Keychain | Store ingest secret |
| Combine | Optional async wrapper |

## Submit.swift

```
import Foundation

struct ContactPayload: Encodable {
  let email: String
  let message: String
  let platform = "ios"
  let _hp = ""
}

func submitContact(email: String, message: String, secret: String) async throws {
  let url = URL(string: "https://www.muxforms.com/api/v1/forms/FORM_UUID/submit")!
  var request = URLRequest(url: url)
  request.httpMethod = "POST"
  request.setValue("application/json", forHTTPHeaderField: "Content-Type")
  request.setValue("Bearer \(secret)", forHTTPHeaderField: "Authorization")
  request.httpBody = try JSONEncoder().encode(ContactPayload(email: email, message: message))
  let (_, response) = try await URLSession.shared.data(for: request)
  guard let http = response as? HTTPURLResponse, http.statusCode == 200 else {
    throw URLError(.badServerResponse)
  }
}
```

## FAQ

### SwiftUI Form?

On submit, call async submit function from .task or Button action.

### App Store privacy?

Disclose contact data collection in privacy nutrition labels.

### macOS Catalyst?

Same URLSession code.

### Widget extension?

Extensions should not embed secrets—open main app contact.

---

HTML version: https://www.muxforms.com/docs/swift
Source: MuxForms (https://www.muxforms.com)
