SDKの初期化
このドキュメントは古いバージョンのiOS SDK v1について記載していますiOS SDK v1は 2021/05/11でサポート終了となります。
SDK v1 からのアップグレードについては、まず SDK v1からv2のアップグレード方法 をご覧ください。
SDK を利用するためには、アプリ起動時に初期化を行う必要があります。
SDK を初期化する
オプションを指定せず初期化する
AppDelegate で KarteTracker をインポートし、application:didFinishLaunchingWithOptions: メソッド内で SDK の初期化を行います。
import KarteTracker
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    KarteTracker.setup(appKey: "YOUR_APP_KEY")
    ...
  }
}#import "AppDelegate.h"
#import <KarteTracker/KarteTracker.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [KarteTracker setupWithAppKey:@"YOUR_APP_KEY"];
  ...
}
@end
Application Key について初期化時に指定する
Application Keyは、KARTE管理画面に記載されているAPI_KEYとは異なります。
キーは営業担当よりお送りしているメールに記載されておりますので、こちらをご確認ください。
オプションを指定して初期化する
初期化時にオプションを指定することで、一部 SDK の挙動を変更することが可能です。
なお利用可能なオプションについては、ページ下部の初期化オプション一覧をご覧ください。
let config = KarteTrackerConfig.configure { (builder) in
  builder.isEnabledTrackingIdfa = false
}
KarteTracker.setup(appKey: "YOUR_APP_KEY", config: config)KarteTrackerConfig *config = [KarteTrackerConfig configureWithBuilder:^(KarteTrackerConfigBuilder * _Nonnull builder) {
  builder.enableTrackingIdfa = NO;
}];
[KarteTracker setupWithAppKey:@"YOUR_APP_KEY" withConfig:config];URLスキームをハンドルする
KARTE で独自に定義したURLスキームを処理するために、URLスキームハンドラを実装する必要があります。
管理画面に表示したQRコードを利用して、SDKの特定の機能を呼び出す際に本実装が必要となります。
なお iOS13 から利用可能な UISceneDelegate を独自に実装している場合は、SceneDelegate base の実装を参照してください。
import KarteTracker
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    KarteUrlSchemeHandler.handle(url)
    return true
  }
}#import "AppDelegate.h"
#import <KarteTracker/KarteTracker.h>
#import <KarteTracker/KarteUrlSchemeHandler.h>
@implementation AppDelegate
  
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  [KarteUrlSchemeHandler handle:url];
  return YES;
}
@endimport UIKit
import KarteTracker
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    KarteUrlSchemeHandler.handle(connectionOptions.urlContexts.first?.url)
  }
  func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    KarteUrlSchemeHandler.handle(URLContexts.first?.url)
  }
}#import "SceneDelegate.h"
#import <UIKit/UIKit.h>
#import <KarteTracker/KarteTracker.h>
#import <KarteTracker/KarteUrlSchemeHandler.h>
  
@interface SceneDelegate () <UIWindowSceneDelegate>
@end
@implementation SceneDelegate
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
  [KarteUrlSchemeHandler handle:connectionOptions.URLContexts.anyObject.URL];
}
- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts
{
  [KarteUrlSchemeHandler handle:URLContexts.anyObject.URL];
}
@end初期化オプション一覧
SDK の初期化時に指定可能なオプションの一覧です。
なお通常はデフォルトの設定で問題ありません。
| オプション | デフォルト値 | 内容 | 
|---|---|---|
| isEnabledTrackingAppLifecycle | true | アプリケーションの初回起動イベント( true で送信されます。 | 
| isEnabledTrackingAppOpen | true | アプリケーション起動イベント( true で送信されます。 | 
| isEnabledTrackingIdfa | false | 広告 ID(IDFA)を送信するかどうかを指定するためのオプション。 true で送信されます。 ※ 1 SDK v1.5.5以上では、 | 
| isEnabledTrackingCrashError | true | アプリケーションのクラッシュイベント( true で送信されます。 | 
| isEnabledFCMTokenResend | false | SDK 初期化時に FCM トークンを自動送信するかどうかを指定するためのオプション。 true で送信されます。 | 
| isEnabledOptOutDefault | false | SDK 初期化時にオプトアウトするかどうかを指定するためのオプション。 true でオプトアウトされます。 | 
| isDryRun | false | イベントの計測を行うかどうかを指定するためのオプション。 true で計測が行われません。 | 
| IDFADelegate | nil | IDFAの取得処理の委譲先インスタンスを指定するオプション。 IDFAの送信が必要な場合は、 | 
| isEnabledVisualTracking | false | ビジュアルトラッキングを有効化にするためのオプション。 trueでビジュアルトラッキングが有効となります。 | 
IDFAの送信方法
v1.5.5 以降でIDFAの送信を行うためには、以下の実装を行う必要があります。
import KarteTracker
import AdSupport
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let config = KarteTrackerConfig.configure { (builder) in
      builder.idfaDelegate = self
    }
    KarteTracker.setup(appKey: "YOUR_APP_KEY", config: config)    
    ...
  }
}
extension AppDelegate: KarteIDFADelegate {
  func isAdvertisingTrackingEnabled() -> Bool {
    return true
  }
    
  func advertisingIdentifierString() -> String? {
    guard ASIdentifierManager.shared().isAdvertisingTrackingEnabled else {
      return nil
    }
    return ASIdentifierManager.shared().advertisingIdentifier.uuidString
  }
}#import "AppDelegate.h"
#import <KarteTracker/KarteTracker.h>
#import <AdSupport/ASIdentifierManager.h>
@interface AppDelegate () <KarteIDFADelegate> 
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  KarteTrackerConfig *config = [KarteTrackerConfig configureWithBuilder:^(KarteTrackerConfigBuilder * _Nonnull builder) {
    builder.IDFADelegate = self;
  }];
  [KarteTracker setupWithAppKey:@"YOUR_APP_KEY" withConfig:config];
  ...
}
- (BOOL)isAdvertisingTrackingEnabled {
  return YES;
}
- (NSString *)advertisingIdentifierString {
  if ([[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled]) {
    return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  }
  return nil;
}
@endなお AdSupport.framework を追加する必要があります。
Build Phases > Link Binary With Libraries からフレームワークの追加を行なってください。
Updated about 1 month ago