---
title: iOS 采集
description: 使用 ASWebAuthenticationSession 在 iOS 应用中运行 Journey 流程的采集，使用 Human Plugin 创建请求返回的链接。
canonical: https://developer.unico.io/zh-CN/human-plugin/integrations/ios
locale: zh-CN
generated_by: markdown-export
---

# iOS 采集

在系统浏览器会话中打开采集，并将用户带回您的应用。

您的后端使用 Journey 流程和一个采用您应用 URL scheme 的 `callbackUri` 在 Human Plugin 中创建流程。应用在 `ASWebAuthenticationSession` 中打开响应里的 `link`。采集结束后，会话通过您的 scheme 返回应用。

请保留创建响应中的 Human Plugin `id`。您的后端用它来读取结果。

## 前提条件

- iOS 13 或更高版本
- Xcode 14 或更高版本
- `AuthenticationServices` 框架（系统内置）
- 您应用独有的 URL scheme，例如 `myapp`

## 1. 使用您的 scheme 创建流程

您的后端将该 scheme 作为 `callbackUri` 发送：

```json
{
  "flow": "humantoken",
  "clientId": "client-a",
  "correlationId": "order-1001",
  "credential": { "type": "DUI_TYPE_NG_BVN", "value": "<document number>" },
  "callbackUri": "myapp://callback"
}
```

并将 `id` 和 `link` 返回给应用。

## 2. 创建 presentation context provider

```swift
import AuthenticationServices
import UIKit

class CaptureContextProvider: NSObject, ASWebAuthenticationPresentationContextProviding {
    func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
        guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
              let window = windowScene.windows.first else {
            return ASPresentationAnchor()
        }
        return window
    }
}
```

## 3. 创建会话管理器

```swift
import AuthenticationServices

class CaptureSessionManager {
    static let shared = CaptureSessionManager()
    private var session: ASWebAuthenticationSession?
    private let contextProvider = CaptureContextProvider()

    /// Calls completion with true when the capture ended, false when the person closed it.
    func start(link: URL, completion: @escaping (Bool) -> Void) {
        let newSession = ASWebAuthenticationSession(
            url: link,
            callbackURLScheme: "myapp"
        ) { callbackURL, _ in
            self.session = nil
            completion(callbackURL != nil)
        }
        newSession.presentationContextProvider = contextProvider
        newSession.prefersEphemeralWebBrowserSession = true
        newSession.start()
        session = newSession
    }
}
```

> **信息:** `callbackURLScheme` 只接受 scheme 名称（`myapp`），不带 `://`。

## 4. 启动采集

```swift
guard let url = URL(string: link) else { return }

CaptureSessionManager.shared.start(link: url) { ended in
    if ended {
        // The capture ended. Ask your back-end to read the outcome
        // with GET /v1/processes/{id}, using the Human Plugin id.
    } else {
        // The person closed the session.
    }
}
```

## 总结

| 文件 | 用途 |
|---|---|
| `CaptureContextProvider.swift` | Presentation context provider |
| `CaptureSessionManager.swift` | `ASWebAuthenticationSession` 生命周期 |

```mermaid
sequenceDiagram
  participant A as Your app
  participant B as Your back-end
  participant H as Human Plugin
  participant S as Browser session
  A->>B: start the capture
  B->>H: POST /v1/processes (Journey flow, callbackUri myapp://callback)
  H-->>B: 201, id + link
  B-->>A: id + link
  A->>S: open link
  Note over S: capture
  S-->>A: myapp://callback
  A->>B: capture ended (id)
  B->>H: GET /v1/processes/{id}
  H-->>B: 200, final state
```