---
title: iOS でのキャプチャ
description: Human Plugin の作成で返された link を使い、ASWebAuthenticationSession で iOS アプリ内に Journey フローのキャプチャを実行します。
canonical: https://developer.unico.io/ja/human-plugin/integrations/ios
locale: ja
generated_by: markdown-export
---

# iOS でのキャプチャ

システムのブラウザセッションでキャプチャを開き、本人をアプリに戻します。

バックエンドは、Journey フローと、アプリの URL スキームを使った `callbackUri` を指定して、Human Plugin にプロセスを作成します。アプリはレスポンスの `link` を `ASWebAuthenticationSession` で開きます。キャプチャが終了すると、セッションはスキームを通じてアプリに戻ります。

作成のレスポンスの Human Plugin `id` を保持してください。バックエンドはこの `id` で結果を読み取ります。

## 前提条件

- iOS 13 以降
- Xcode 14 以降
- `AuthenticationServices` フレームワーク(標準搭載)
- `myapp` のような、アプリ固有の URL スキーム

## 1. スキームを指定してプロセスを作成する

バックエンドはスキームを `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. プレゼンテーションコンテキストプロバイダを作成する

```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()

    /// キャプチャが終了した場合は true、本人が閉じた場合は false で completion を呼び出します。
    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` にはスキーム名(`myapp`)のみを指定します。`://` は付けません。

## 4. キャプチャを開始する

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

CaptureSessionManager.shared.start(link: url) { ended in
    if ended {
        // キャプチャが終了しました。バックエンドに、Human Plugin の id を使った
        // GET /v1/processes/{id} で結果を読み取るよう依頼します。
    } else {
        // 本人がセッションを閉じました。
    }
}
```

## まとめ

| ファイル | 用途 |
|---|---|
| `CaptureContextProvider.swift` | プレゼンテーションコンテキストプロバイダ |
| `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
```