# \[iOS]KARTE独自のカスタムURLスキームをハンドリングできるようにする

KARTE SDKでは、下記の各種機能に対応するために、カスタムURLスキームをハンドリングできるようにする設定が必要です。

* [Native機能呼び出し](https://support.karte.io/post/1F1VeY2yy3mrTIO2U4HhHi)
  * [アクションからNative機能を呼び出す](/ios-sdk-appendix/appendix-native-command-ios-sdk.md)
* [ビジュアルトラッキング](https://support.karte.io/post/7JbUVotDwZMvl6h3HL9Zt7)
* [アプリで確認](https://support.karte.io/post/3VsAULpSA8GmHwHXRHlB2O#2-7) の機能を使ったアクションのプレビュー

下記機能では「2. カスタムURLスキームハンドラを追加する」のみ対応が必要です。

* [SDKイベントデバッガーを利用する](https://support.karte.io/post/qTf6oZNjT5UzCv16lIPY6)
* [サイト、アプリを閲覧している自分自身を探す](https://support.karte.io/post/5IJjR3XbaIA4yVK1drqoRB)

## 1. カスタムURLスキームを設定する

任意のエディタで `Info.plist` を開き、下記設定を追記します。

{% code title="Info.plist" overflow="wrap" %}

```xml
<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>krt-$(APIキー)</string>
    </array>
  </dict>
</array>
```

{% endcode %}

なお `$(APIキー)` の部分には、KARTEプロジェクト毎に用意されているAPIキーを指定します。

{% hint style="info" %}
**APIキーの確認方法**

APIキーは管理画面から取得可能です。\
詳細については、[こちら](https://support.karte.io/post/7bq63EfIXdTjcHXextXKBh)をご覧ください。
{% endhint %}

{% hint style="warning" %}
**間違いやすい点**

SDKの初期化に利用するアプリケーションキーとAPIキーは異なるものです。\
お間違えないようご注意ください。
{% endhint %}

## 2. カスタムURLスキームハンドラを追加する

KARTE側でURLスキームを処理するために、`application(_:open:options:)` メソッド内にハンドラを追加します。

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

```swift
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
  return KarteApp.application(app, open: url)
}
```

{% endcode %}
{% endtab %}

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

```objc
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
  return [KRTApp application:app openURL:url];
}
```

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

なお iOS13 から利用可能な `UISceneDelegate` を実装している場合は、`scene(_:willConnectTo:options:)` および `scene(_:openURLContexts:)` メソッド内にハンドラを追加します。

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

```swift
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
  if let context = connectionOptions.urlContexts.first {
    KarteApp.application(app, open: context.url)
  }
}

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
  if let context = URLContexts.first {
    KarteApp.application(app, open: context.url)
  }
}
```

{% endcode %}
{% endtab %}

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

```objc
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
  UIOpenURLContext *URLContext = connectionOptions.URLContexts.anyObject;
  if (URLContext) {
    [KRTApp application:[UIApplication sharedApplication] openURL:URLContext.URL];
  }
}

- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts {
  UIOpenURLContext *URLContext = URLContexts.anyObject;
  if (URLContext) {
    [KRTApp application:[UIApplication sharedApplication] openURL:URLContext.URL];
  }
}
```

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

{% hint style="info" %}
**`application(_:open:options:)` メソッドの戻り値について**

KARTE SDK で処理可能なカスタムURLスキームが渡された場合は戻り値として `true` を返し、反対に処理できないカスタムURLスキームが渡された場合は `false` を返します。
{% endhint %}


---

# 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-appendix/concepts-custom-url-scheme-ios-sdk.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.
