---
title: Captura no iOS
description: Execute a captura de um fluxo Journey em um app iOS com ASWebAuthenticationSession, usando o link retornado pelo create do Human Plugin.
canonical: https://developer.unico.io/pt-BR/human-plugin/integrations/ios
locale: pt-BR
generated_by: markdown-export
---

# Captura no iOS

Abra a captura em uma sessão de navegador do sistema e traga a pessoa de volta ao seu app.

Seu back-end cria o processo no Human Plugin com um fluxo Journey e um `callbackUri` que usa o URL scheme do seu app. O app abre o `link` da resposta em uma `ASWebAuthenticationSession`. Quando a captura termina, a sessão retorna ao app pelo seu scheme.

Guarde o `id` do Human Plugin retornado pelo create. Seu back-end lê o resultado com ele.

## Pré-requisitos

- iOS 13 ou superior
- Xcode 14 ou superior
- O framework `AuthenticationServices` (nativo)
- Um URL scheme exclusivo do seu app, como `myapp`

## 1. Crie o processo com o seu scheme

Seu back-end envia o scheme como `callbackUri`:

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

e devolve `id` e `link` ao app.

## 2. Crie o 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. Crie o gerenciador de sessão

```swift
import AuthenticationServices

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

    /// Chama completion com true quando a captura terminou, false quando a pessoa a fechou.
    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
    }
}
```

> **Informação:** `callbackURLScheme` recebe apenas o nome do scheme (`myapp`), sem `://`.

## 4. Inicie a captura

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

CaptureSessionManager.shared.start(link: url) { ended in
    if ended {
        // A captura terminou. Peça ao seu back-end para ler o resultado
        // com GET /v1/processes/{id}, usando o id do Human Plugin.
    } else {
        // A pessoa fechou a sessão.
    }
}
```

## Resumo

| Arquivo | Finalidade |
|---|---|
| `CaptureContextProvider.swift` | Presentation context provider |
| `CaptureSessionManager.swift` | Ciclo de vida da `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
```