# AES (Advanced Electronic Signature)

The AES (Advanced Electronic Signature) module shows the user the documents to sign, collects their consent, and captures a certificate-backed electronic signature. That digital certificate verifies their identity, making the signed document legally binding and compliant.

For an overview of this module and how it works, see [Advanced Electronic Signature](https://developer.incode.com/docs/advanced-electronic-signature).

How you use this module depends on your integration pattern. When the app defines the steps in code, you add the module to an `IncdOnboardingFlowConfiguration` as shown below; when the flow is defined in Dashboard, you reference it by session token and let the back end drive the steps. See [Integration Approaches](https://developer.incode.com/docs/ios-flow-configuration).

**Availability**: All variants.

## Add AES

Add the module to your `IncdOnboardingFlowConfiguration` with the single `addAes(configuration:showCertificateOnSuccess:)` method:

```swift
// Default configuration
flowConfig.addAes(showCertificateOnSuccess: false)

// With a custom configuration
flowConfig.addAes(
    configuration: AESConfiguration(uploadDocument: true, downloadDocument: true),
    showCertificateOnSuccess: true
)
```

- `configuration` is optional. It defaults to `nil`. Pass an `AESConfiguration` to control document upload and download. See [Configuration Options](#configuration-options).
- `showCertificateOnSuccess: Bool` is required. When `false`, the module shows only the success screen and then closes automatically; when `true`, it shows a preview of the signed documents after signing succeeds.

### Example

The example below builds a custom `AESConfiguration` that shows the upload screen and downloads the signed PDF, adds the AES module to the flow, starts onboarding, and receives the outcome through the `onAESCompleted` delegate callback.

```swift
let flow = IncdOnboardingFlowConfiguration()
flow.addAes(
    configuration: AESConfiguration(uploadDocument: true, downloadDocument: true),
    showCertificateOnSuccess: true
)

IncdOnboardingManager.shared.startOnboarding(
    sessionConfig: IncdOnboardingSessionConfiguration(token: "<SESSION_TOKEN>"),
    flowConfig: flow,
    delegate: self
)

// ...

extension MyViewController: IncdOnboardingDelegate {
    func onAESCompleted(result: AESResult) {
        if let error = result.error {
            // Handle AESError
        } else if result.success {
            // Documents signed successfully
        }
    }
}
```

## Configuration Options

Configure the module by passing an `AESConfiguration` to `addAes(configuration:)`.

| Option             | Type    | Description                                                                                                                                      |
| ------------------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `uploadDocument`   | `Bool?` | When `true`, shows the AES upload screen before the standard AES screens. When left `nil`, treated as `false`.                                   |
| `downloadDocument` | `Bool?` | When `true`, downloads the signed PDF and shows the confirmed signature details at the end of the AES flow. When left `nil`, treated as `false`. |

`showCertificateOnSuccess` is passed directly to `addAes(...)` instead of through `AESConfiguration`; see [Add AES](#add-aes).

## Result

The module delivers an `AESResult` to the `onAESCompleted(result:)` callback on your `IncdOnboardingDelegate`. `onAESCompleted` is always called, whether or not signing succeeded, and the user then continues with the rest of the flow.

`AESResult` fields:

- `success: Bool`: `true` if the documents were signed successfully.
- `error: AESError?`: The signing error, or `nil` if there was no error.

## Errors

`AESError` cases:

- `noDocuments`: No documents were available for the session.
- `failedToSign`: Signing failed.

<br />
