> For the complete documentation index, see [llms.txt](https://docs.ecommerce.seb.ee/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ecommerce.seb.ee/seb-docs-draft/seb-docs-public-lt/integracija/mobilusis-sdk/ios-sdk/naudojant-sdk/objective-c-ir-swiftui.md).

# Objective-C ir SwiftUI

Kreipkitės į SDK iš Objective-C, SwiftUI ir asinchroninio kodo, o vietoje užbaigimo bloko naudokite delegatą.

Naudokite šį vadovą, jei jūsų iškvietimo vieta nėra paprastas Swift `UIViewController`.

Viešasis API visur yra suderinamas su Objective-C, būtent tai leidžia palaikyti Objective-C ir kartu atmeta tik Swift arba tik SwiftUI sąsają. Toliau pateikti tiltai užpildo šią spragą.

***

## Objective-C

Objective-C kalboje tipai turi `EGW` priešdėlį. `PaymentKit` yra `EGWPaymentKit`, `Konfigūracija` yra `EGWConfiguration`, ir taip toliau.

### Talpinamas atsiskaitymas

```objc
@import EGWPay;

EGWConfiguration *configuration =
    [[EGWConfiguration alloc] initWithApiBaseURL:[NSURL URLWithString:@"https://your-backend.example.com"]
                                     environment:EGWEnvironmentSandbox];

EGWPaymentKit *paymentKit = [[EGWPaymentKit alloc] initWithConfiguration:configuration];

EGWPaymentRequest *request =
    [[EGWPaymentRequest alloc] initWithPaymentId:paymentId
                                 verificationKey:verificationKey
                                     checkoutURL:checkoutURL
                                       returnURL:[NSURL URLWithString:@"myapp://checkout/return"]];

[paymentKit startPayment:request from:self completion:^(EGWPaymentResult *result) {
    switch (result.outcome) {
        case EGWPaymentOutcomeSucceeded:
            [self showReceiptWithReference:result.orderNumber ?: result.paymentId];
            break;
        case EGWPaymentOutcomeFailed:
            [self showFailure:result.error];
            break;
        case EGWPaymentOutcomeCancelled:
            break;
        case EGWPaymentOutcomeUnresolved:
            [self confirmWithBackend:result.paymentId];
            break;
    }
}];
```

### Natyvioji kortelės forma

```objc
EGWConfiguration *configuration =
    [[EGWConfiguration alloc] initWithApiBaseURL:[NSURL URLWithString:@"https://your-backend.example.com"]
                              checkoutAPIBaseURL:[NSURL URLWithString:@"https://sandbox-api.example.com"]
                                 checkoutBaseURL:[NSURL URLWithString:@"https://sandbox-checkout.example.com"]
                                     environment:EGWEnvironmentSandbox];

EGWPaymentKit *paymentKit = [[EGWPaymentKit alloc] initWithConfiguration:configuration];

EGWCardInput *card = [[EGWCardInput alloc] init];
card.cardholderName = self.nameField.text;
card.cardNumber = self.numberField.text;
card.expiryMonth = self.expiryField.month;
card.expiryYear = self.expiryField.year;
card.securityCode = self.securityCodeField.text;

EGWCardPaymentRequest *request =
    [[EGWCardPaymentRequest alloc] initWithPaymentId:paymentId
                                     verificationKey:verificationKey];

[paymentKit startCardPayment:request card:card from:self completion:^(EGWPaymentResult *result) {
    // Tas pats rezultatų apdorojimas kaip aukščiau.
}];
```

### Delegato naudojimas vietoje bloko

Abu įėjimo taškai turi ir versiją su delegatu. Delegatas laikomas silpnai, todėl savo kode turėkite stiprią nuorodą į jį.

```objc
@interface CheckoutViewController () <EGWPaymentDelegate>
@end

@implementation CheckoutViewController

- (void)pay {
    [self.paymentKit startPayment:self.request from:self delegate:self];
}

- (void)paymentDidFinish:(EGWPaymentResult *)result {
    // Iškviečiama vieną kartą, pagrindinėje gijoje, po to, kai SDK sąsaja uždaroma.
}

@end
```

Tas pats atitikmuo yra ir Swift: `startPayment(_:from:delegate:)` ir `startCardPayment(_:card:from:delegate:)` pasitelkite `PaymentDelegate` ir iškvieskite `paymentDidFinish(_: )`.

***

## SwiftUI

`startPayment` pateikiama iš `UIViewController`, kurio SwiftUI tiesiogiai nesuteikia. Sujunkite jį vieną kartą:

```swift
import SwiftUI
import UIKit

/// Atveria artimiausią UIKit view controller SwiftUI kodui.
struct ViewControllerProvider: UIViewControllerRepresentable {
    final class Holder: UIViewController {}

    @Binding var controller: UIViewController?

    func makeUIViewController(context: Context) -> Holder {
        let holder = Holder()
        DispatchQueue.main.async { controller = holder }
        return holder
    }

    func updateUIViewController(_ uiViewController: Holder, context: Context) {}
}
```

Tada naudokite jį ten, kur pradedate mokėjimą:

```swift
struct CheckoutButton: View {
    @State private var presenter: UIViewController?
    let request: PaymentRequest
    let paymentKit: PaymentKit

    var body: some View {
        Button("Mokėti") {
            guard let presenter else { return }
            paymentKit.startPayment(request, from: presenter) { result in
                // Apdorokite rezultatą.
            }
        }
        .background(ViewControllerProvider(controller: $presenter).frame(width: 0, height: 0))
    }
}
```

**Palikite laisvą pateikimo vietą.** Jei jūsų atsiskaitymo ekranas pats yra `.sheet` arba `.fullScreenCover`, iš jo pateikiant SDK tai gali nepavykti. Vietoje to pateikite atsiskaitymo ekraną.

### Sujungimas į `async`

SDK veikia pagal užbaigimo funkciją. Apgaubkite jį vieną kartą:

```swift
func pay(_ request: PaymentRequest, from presenter: UIViewController) async -> PaymentResult {
    await withCheckedContinuation { continuation in
        paymentKit.startPayment(request, from: presenter) { continuation.resume(returning: $0) }
    }
}
```

Tai saugu būtent todėl, kad užbaigimas įvyksta lygiai vieną kartą. Tęsinio atnaujinimas du kartus sukeltų strigtį, o iškvietimo sutartis to neleidžia.

### Kortelės teksto laukų apgaubimas

Kortelės teksto laukai yra įprasti `UITextField` poklasiai, todėl jie apgaubiami kaip ir visi kiti:

```swift
struct CardNumberField: UIViewRepresentable {
    @Binding var text: String

    func makeUIView(context: Context) -> CardNumberTextField {
        let field = CardNumberTextField()
        field.addTarget(context.coordinator,
                        action: #selector(Coordinator.changed),
                        for: .editingChanged)
        return field
    }

    func updateUIView(_ uiView: CardNumberTextField, context: Context) {}

    func makeCoordinator() -> Coordinator { Coordinator(text: $text) }

    final class Coordinator: NSObject {
        @Binding var text: String
        init(text: Binding<String>) { _text = text }
        @objc func changed(_ sender: CardNumberTextField) { text = sender.text ?? "" }
    }
}
```

***

## Tolimesni žingsniai

* [API dokumentacija](/seb-docs-draft/seb-docs-public-lt/integracija/mobilusis-sdk/ios-sdk/nuoroda/api.md) - kiekvienas tipas pateikiamas tiek Swift, tiek Objective-C pavadinimu.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.ecommerce.seb.ee/seb-docs-draft/seb-docs-public-lt/integracija/mobilusis-sdk/ios-sdk/naudojant-sdk/objective-c-ir-swiftui.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
