# \[iOS]SwiftUI向けの対応

## SDKの初期化コードを追加する

SwiftUI を使用している場合は、プロジェクト作成時には UIApplicationDelegate を継承した AppDelegate クラスがないため、AppDelegate を作成し UIApplicationDelegateAdaptor 等を利用して `App` 構造体に接続する必要があります。

{% code title="MyApp.swift" overflow="wrap" %}

```swift
@main
struct MyApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
}
```

{% endcode %}

この際、AppDelegate クラスは App 構造体よりも後に初期化されるため、現状のSDKでは `App.init()` 内等で直接KARTEのイベントを発行することはできません。 `AppDelegate.application(_:didFinishLaunchingWithOptions:)` の後にイベントを発行するようにしてください。

作成したAppDelegateにおいて`application(_:didFinishLaunchingWithOptions:)` メソッド内に初期化コードを追加します。

{% tabs %}
{% tab title="Swift" %}
{% code title="AppDelegate.swift" overflow="wrap" %}

```swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  KarteApp.setup()
}
```

{% endcode %}
{% endtab %}

{% tab title="Objective-C" %}
{% code title="AppDelegate.m" overflow="wrap" %}

```objc
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [KRTApp setupWithConfiguration:KRTConfiguration.default_];
  ...
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

## ディープリンクによってアプリが起動した際に`deep_link_app_open`イベントを発生させる

### 対応が必要な場合

SwiftUIでディープリンクを介してURLを受け取った際に適切なイベントを発火させたい場合は、下記の対応が必要になります。

### 推奨される実装

SwiftUIで推奨される実装方法は、`onOpenURL` modifierを使ってアプリケーション全体でディープリンクを捕捉する方法です。このmodifier内で `KarteApp.application(_:open:)` メソッドを呼び出し、外部からのURL schemeを適切に処理します。

#### ステップ1: AppDelegateの準備

UIApplicationDelegateAdaptor 属性を使用して、`AppDelegate` クラスをSwiftUIライフサイクルに統合します。

{% code title="SampleApp.swift" overflow="wrap" %}

```swift
@main
struct SampleApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    // ...
}
```

{% endcode %}

#### ステップ2: onOpenURL modifier の使用

WindowGroup の直下で `onOpenURL` modifierを使用し、URLが開かれたときの処理を定義します。

{% code title="SampleApp.swift" overflow="wrap" %}

```swift
var body: some Scene {
    WindowGroup {
        NavigationView {
            ContentView()
            // 他のView
        }
        .onOpenURL { url in
            // URLを受け取ったときの処理
            KarteApp.application(UIApplication.shared, open: url)
            // その他の必要な処理
        }
    }
}
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://app.developers.karte.io/ios-sdk/implementation-for-swift-ui.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
