# \[iOS]広告ID（IDFA）を送信する

KARTE SDKによる広告ID（IDFA）の送信を有効にしたい場合は、本記事を参考に実装をしてください。

なおSDKには広告IDを直接取得する機能はありません。\
そのためアプリケーション側に広告IDを取得する処理を実装する必要があります。

{% hint style="warning" %}
**広告IDの送信について**

広告IDの送信にはいくつか注意事項がございます。\
送信処理を実装する前に、まず [こちら](https://support.karte.io/post/13v6pgNyNKtrxyIvYZcnAs) の記事をご覧ください。
{% endhint %}

## 事前準備

`AdSupport.framework` をビルドターゲットに追加してください。

## 実装

1. Delegateを実装する\
   SDK が提供する `IDFADelegate` を実装します。\
   ここでは例として AppDelegate に実装する形で進めます。

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

```swift
import AdSupport
import KarteCore
import AppTrackingTransparency

...

extension AppDelegate: IDFADelegate {
  var isAdvertisingTrackingEnabled: Bool {
    if #available(iOS 14, *) {
        return ATTrackingManager.trackingAuthorizationStatus == .authorized
    } else {
        return ASIdentifierManager.shared().isAdvertisingTrackingEnabled
    }
  }

  var advertisingIdentifierString: String? {
    return ASIdentifierManager.shared().advertisingIdentifier.uuidString
  }
}
```

{% endcode %}
{% endtab %}

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

```objc
#import "AppDelegate.h"
#import <AdSupport/AdSupport.h>
#import <AppTrackingTransparency/AppTrackingTransparency.h>
@import KarteCore;

@interface AppDelegate () <KRTIDFADelegate>
@end

@implementation AppDelegate

...

- (BOOL)isAdvertisingTrackingEnabled {
    if (@available(iOS 14.0, *)) {
        return ATTrackingManager.trackingAuthorizationStatus == ATTrackingManagerAuthorizationStatusAuthorized;
    } else {
        return [[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled];
    }
}

- (NSString *)advertisingIdentifierString {
    return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
}

@end
```

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

2. SDKの初期化を行う\
   初期化オプションを指定してSDKの初期化を行います。

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

```swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  let configuration = Configuration { (configuration) in
    configuration.idfaDelegate = self
  }
  KarteApp.setup(appKey: "アプリケーションキー", configuration: configuration)
  ...
}
```

{% endcode %}
{% endtab %}

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

```objc
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  KRTConfiguration *configuration = [KRTConfiguration configWithConfigurator:^(KRTConfiguration * _Nonnull configuration) {
    [configuration setIdfaDelegate:self];
  }];
  [KRTApp setupWithAppKey:@"アプリケーションキー" configuration:configuration];
  ...
}
```

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

## 確認

任意のイベントの `app_info.system_info.idfa` フィールドに値が含まれていれば、正しく実装されております。

詳細な確認方法については、[イベント画面について](https://support.karte.io/post/6avC4CUCE3Dm9PydcGFfgM) をご覧ください。


---

# 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/appendix-idfa-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.
