> 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-lv/integracija/mobilais-sdk/ios-sdk/sdk-izmantosana/objective-c-un-swiftui.md).

# Objective-C un SwiftUI

Izsauciet SDK no Objective-C, no SwiftUI un no asinhronā koda, un izmantojiet delegātu pabeigšanas bloka vietā.

Izmantojiet šo rokasgrāmatu, ja jūsu izsaukuma vieta nav vienkāršs Swift `UIViewController`.

Publiskais API visā tā apjomā ir saderīgs ar Objective-C, un tieši tas padara iespējamu Objective-C atbalstu un izslēdz tikai Swift vai tikai SwiftUI saskarni. Tālāk redzamie tilti aizpilda šo plaisu.

***

## Objective-C

Objective-C valodā tipi nes `EGW` prefiksu. `PaymentKit` ir `EGWPaymentKit`, `Configuration` ir `EGWConfiguration`un tā tālāk.

### Mitinātais norēķins

```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;
    }
}];
```

### Vietējā kartes 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) {
    // Tāda pati rezultāta apstrāde kā iepriekš.
}];
```

### Delegāta izmantošana bloka vietā

Abiem ieejas punktiem ir arī pārslodze ar delegātu. Delegāts tiek glabāts ar vāju atsauci, tāpēc saglabājiet savu spēcīgu atsauci uz to.

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

@implementation CheckoutViewController

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

- (void)paymentDidFinish:(EGWPaymentResult *)result {
    // Tiek izsaukts vienu reizi galvenajā pavedienā pēc tam, kad SDK saskarne ir aizvērta.
}

@end
```

Tāds pats pāris pastāv arī Swift: `startPayment(_:from:delegate:)` un `startCardPayment(_:card:from:delegate:)` ņemiet `PaymentDelegate` un izsauciet `paymentDidFinish(_:)`.

***

## SwiftUI

`startPayment` tiek prezentēts no `UIViewController`, ko SwiftUI jums tieši nenodod. Veiciet tiltošanu vienu reizi:

```swift
import SwiftUI
import UIKit

/// Atklāj tuvāko UIKit skata kontrolieri SwiftUI kodam.
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) {}
}
```

Pēc tam izmantojiet to ikreiz, kad uzsākat maksājumu:

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

    var body: some View {
        Button("Maksāt") {
            guard let presenter else { return }
            paymentKit.startPayment(request, from: presenter) { result in
                // Apstrādājiet rezultātu.
            }
        }
        .background(ViewControllerProvider(controller: $presenter).frame(width: 0, height: 0))
    }
}
```

**Atstājiet brīvu prezentēšanas vietu.** Ja jūsu norēķinu ekrāns pats atrodas iekš `.sheet` vai `.fullScreenCover`, SDK prezentēšana no tā var neizdoties. Tā vietā atveriet norēķinu ekrānu.

### Tiltošana uz `async`

SDK ir balstīts uz pabeigšanas callback. Aptiniet to vienreiz:

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

Tas ir droši tieši tāpēc, ka pabeigšanas callback tiek izsaukts tieši vienu reizi. Turpinājuma atsākšana divreiz izraisītu avāriju, un callback līgums to izslēdz.

### Kartes teksta laukumu ietīšana

Kartes teksta lauki ir parasti `UITextField` apakšklases, tāpēc tie tiek aptīti tāpat kā jebkurš cits:

```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 ?? "" }
    }
}
```

***

## Nākamie soļi

* [API atsauce](/seb-docs-draft/seb-docs-public-lv/integracija/mobilais-sdk/ios-sdk/atsauce/api.md) - katrs tips gan tā Swift, gan Objective-C nosaukumā.


---

# 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-lv/integracija/mobilais-sdk/ios-sdk/sdk-izmantosana/objective-c-un-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.
