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

> ## Agent Instructions
> Test in the sandbox first: base URL https://api-sandbox.y.uno (Test Mode in the Dashboard). Production is https://api.y.uno (Live Mode).
> Every API request needs the public-api-key and private-secret-key headers. Get them in the Dashboard at https://dashboard.y.uno/developers (Developers > Credentials); sandbox and production keys are different.
> Send an X-Idempotency-Key (a UUID) when creating payments, refunds and cancels, so retries do not create duplicates.
> Start with the Quickstart (https://docs.y.uno/docs/quickstart.md) and the agent guide (https://docs.y.uno/agents.md). To search these docs from a coding agent, connect the docs MCP server at https://docs.y.uno/mcp.

# iOS 注册

> 在 iOS 上使用 full-checkout 注册支付方式，通过注册 delegate 处理结果

> [!NOTE]
>
> **This page documents a legacy SDK.** Starting a new integration? Use the [Yuno SDK](https://docs.y.uno/docs/yuno-sdk/get-started.md). Already on a legacy SDK? Follow the [migration guide](https://docs.y.uno/docs/yuno-sdk/migration/overview.md).
>
> The new **Yuno SDK** (Web v2.x, iOS v3.x, Android v3.x, React Native v2.x) replaces the Full, Lite, Seamless, Render, Headless, and Secure Fields SDKs with one API across every platform. Legacy SDKs remain supported side by side during the transition and are scheduled for deprecation in **2027**. Contact your Account Manager for migration planning.

iOS SDK 让您可以轻松实现注册流程，将支付方式保存到客户账户。

按照与 [full-checkout](https://docs.y.uno/docs/sdks/full-checkout/ios-payments#include-the-library-in-your-project) 相同的步骤在项目中引入库。

## 其他资源

* 如果您不确定应采用哪种流程，请参阅[选择适合您的集成方式](https://docs.y.uno/zh-hans/docs/sdks/overview/choose-integration.md)。

* 查看[发布说明](https://docs.y.uno/changelog/ios.md)或访问 [Yuno iOS SDK 代码库](https://github.com/yuno-payments/yuno-sdk-ios)，确认可用的最新 SDK 版本。

* [full-checkout 注册（iOS）](#full-checkout-enrollment-ios)：使用预构建 UI 组件自动注册

## 要求

* [Swift Package Manager](https://www.swift.org/package-manager/)
* iOS 14.0 或更高版本
* 有效的 Yuno 账户，以及 API 凭证（从 [Yuno Dashboard](https://dashboard.y.uno/) → **Developers** > **Credentials** 获取）
* 注册前，使用[创建客户 endpoint](https://docs.y.uno/reference/customers/create-customer.md) 创建客户

## 参数

有关参数的完整列表，请参阅 [iOS SDK 通用参考](https://docs.y.uno/zh-hans/docs/sdks/resources/references/ios.md)。

| 参数 | 说明 |
| - | - |
| `customerSession` | 来自创建客户会话 API 的客户会话 ID。必填。 |
| `countryCode` | ISO 国家代码（例如 `BR`）。必填。 |
| `language` | UI 的语言代码。可选。 |
| `viewController` | 用于展示注册流程的 UIViewController。使用 delegate 时必填。 |
| `yunoEnrollmentResult(_:)` | Delegate：注册完成并返回结果。 |
| `YunoConfig`（初始化） | 可选：`appearance`、`saveCardEnabled`。请参阅通用参考。 |

## full-checkout 注册（iOS）

Yuno full-checkout iOS SDK 提供带预构建 UI 的注册、卡片注册、状态处理和基本错误管理。请参阅上文的[要求](#requirements)部分。

### 步骤 1：创建客户和客户会话

在注册支付方式之前，使用[创建客户 endpoint](https://docs.y.uno/reference/customers/create-customer.md) 在 Yuno 系统中创建客户。此 endpoint 会返回 `customer_id`，您将用它把已注册的支付方式与特定客户关联。

然后使用[创建客户会话](https://docs.y.uno/reference/customer-sessions-enrollment/create-customer-session.md) endpoint 创建客户会话。调用注册方法时将使用该会话信息。

```json theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
POST /v1/customer-session

{
  "country": "BR",
  "customer_id": "6c85a4e3-0a6c-423d-a12a-10045320ab4a"
}
```

响应中包含 `customer_session` ID，您将在下一步中使用它。

同时，在您的服务器上为该客户会话创建[注册支付方式对象](https://docs.y.uno/reference/payment-methods-checkout/enroll-payment-method-checkout.md)，以定义客户可以注册哪种支付方式。请将您的私有 API 密钥保存在服务器上。

### 步骤 2：在项目中引入库

在项目中引入库的方式与支付流程相同。请按照其中[在项目中引入库](https://docs.y.uno/docs/sdks/full-checkout/ios-payments#include-the-library-in-your-project)的步骤操作。

#### Swift Package Manager

YunoSDK 已不再发布到 CocoaPods，最后一个 CocoaPods 版本是 2.30.0。

```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
dependencies: [
    .package(url: "https://github.com/yuno-payments/yuno-sdk-ios.git", .upToNextMajor(from: "2.31.0"))
]
```

将 `YunoSDK`（动态）或 `YunoSDK-Static`（静态，即 CocoaPods 提供的形式）添加到您的应用 target，切勿同时添加两者。

查看[发布说明](https://docs.y.uno/changelog/ios.md)或 [Yuno iOS SDK 代码库](https://github.com/yuno-payments/yuno-sdk-ios)，了解当前的 SDK 版本。

### 步骤 3：使用公钥初始化 SDK

初始化 SDK：

1. 从 [Yuno Dashboard](https://dashboard.y.uno/) 获取您的公共 API 密钥
2. 使用您的 API 密钥调用 `Yuno.initialize()` 初始化 SDK：

```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
import YunoSDK

Yuno.initialize(
    apiKey: "PUBLIC_API_KEY",
    config: YunoConfig(),
    callback: { print("Yuno SDK initialized") }
)
```

使用 `YunoConfig` 数据类自定义 SDK 的行为。调用 `Yuno.initialize()` 时请包含此配置。例如：

```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
struct YunoConfig {
    var appearance: Appearance? = nil
    var saveCardEnabled: Bool = false
}
```

### 步骤 4：实现注册 delegate

创建一个采用 `YunoEnrollmentDelegate` 协议的类：

> [!NOTE]
>
> **Swift 6 并发要求**
>
> 如果您使用 Swift 6，在实现 `YunoEnrollmentDelegate` 协议时需要考虑特定的并发问题。有关详细的实现选项和最佳实践，请参阅 [Swift 6 并发](https://docs.y.uno/zh-hans/docs/sdks/resources/swift-6-concurrency.md)指南。

```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
protocol YunoEnrollmentDelegate: AnyObject {
    var customerSession: String { get }
    var countryCode: String { get }
    var language: String? { get }
    var viewController: UIViewController? { get }

    func yunoEnrollmentResult(_ result: Yuno.Result)
}

class ViewController: UIViewController, @preconcurrency YunoEnrollmentDelegate {
    var customerSession: String { "cus_ses_123456" }
    var countryCode: String { "BR" }
    var language: String? { "en" }
    var viewController: UIViewController? { self }

    func yunoEnrollmentResult(_ result: Yuno.Result) {
        switch result.status {
        case .succeeded:
            print("Enrollment successful")
        case .fail:
            print("Enrollment failed")
        case .processing:
            print("Enrollment processing")
        case .reject:
            print("Enrollment rejected")
        case .userCancelled:
            print("User canceled")
        case .internalError:
            print("Internal error")
        @unknown default:
            break
        }
    }
}
```

#### 选项

| 参数 | 说明 |
| - | - |
| `customerSession` | 客户会话的唯一标识符。 |
| `countryCode` | 进行注册的国家代码。有关支持的国家，请参阅[国家覆盖范围](https://docs.y.uno/zh-hans/docs/sdks/resources/country-coverage.md)。 |
| `language` | 注册表单的语言代码（例如 `"en"`、`"es"`、`"pt"`）。请参阅[支持的语言](https://docs.y.uno/zh-hans/docs/sdks/resources/languages-supported.md)。 |
| `viewController` | 用于展示注册流程的 `UIViewController`。正确展示 UI 时必需。 |
| `yunoEnrollmentResult(_:)` | 注册流程完成并返回最终结果时调用。 |

### 步骤 5：启动注册流程

调用 `enrollPayment()` 方法以显示注册流程：

```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
func startEnrollment() {
    Yuno.enrollPayment(with: self, showPaymentStatus: true)
}
```

SDK 使用您在 delegate 中提供的 `viewController`，以模态方式展示一个全屏 `UIViewController`。在 SwiftUI 中，请封装一个 `UIViewController`，并通过 `viewController` 属性返回它。

#### 选项

| 参数 | 类型 | 说明 |
| - | - | - |
| `delegate` | `YunoEnrollmentDelegate` | 处理注册 callback 的 delegate 对象。 |
| `showPaymentStatus` | `Bool` | 是否在注册过程中显示状态视图。为 `true` 时，SDK 显示默认状态页面。为 `false` 时，您需要通过 callback 自行处理状态显示。 |

在 SwiftUI 中，请封装一个 `UIViewController`，并从 `viewController` 属性返回它，以便 SDK 展示 UI。

### 步骤 6：处理 deep link 返回（可选）

仅当注册流程使用 deep link 时才需要。如果您的支付方式不使用 deep link，请跳过此步骤。否则，请在您的 `AppDelegate` 中处理返回：

```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
func application(_ app: UIApplication,
                 open url: URL,
                 options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {

    guard url.scheme == "yunoexample" else { return false }

    return Yuno.receiveDeeplink(url)
}
```

请确保 `url.scheme` 与创建 `customer_session` 时使用的 `callback_url` 一致。

### 步骤 7：处理注册结果

SDK 会调用您的 `yunoEnrollmentResult(_:)` delegate 方法并传入最终状态：

```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
func yunoEnrollmentResult(_ result: Yuno.Result) {
    switch result.status {
    case .succeeded:
        print("Enrollment successful")
        // Navigate to success screen or update UI
    case .fail:
        print("Enrollment failed")
        // Show error message
    case .processing:
        print("Enrollment still processing")
        // Show processing message
    case .reject:
        print("Enrollment rejected")
        // Show rejection message
    case .userCancelled:
        print("User canceled")
        // Handle cancellation
    case .internalError:
        print("Internal error occurred")
        // Show error message
    @unknown default:
        break
    }
}
```

#### 注册结果状态

| 状态 | 说明 | 是否需要操作 |
| - | - | - |
| `succeeded` | 注册成功完成 | 否 |
| `fail` | 由于验证或技术问题导致注册失败 | 是：调查原因并重试 |
| `processing` | 注册处理中，等待批准 | 否 |
| `reject` | 注册被拒绝（数据无效、检测到欺诈等） | 是：告知用户并建议后续操作 |
| `internalError` | 发生意外的内部错误 | 是：需要技术介入 |
| `userCancelled` | 用户取消了注册 | 否 |

`Yuno.Result` 不包含令牌。除了高层级的 `status` 外，它还提供 `substatus` 和 `message`（SDK 2.27.0 及更高版本）。

### 补充功能

有关样式、主题、表单选项和其他配置，请参阅 [SDK 自定义](https://docs.y.uno/zh-hans/docs/sdks/customization/ios.md)。

## 通用参考

有关完整的参数和自定义详情，请参阅 [iOS SDK 通用参考](https://docs.y.uno/zh-hans/docs/sdks/resources/references/ios.md)。


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