SDKの実装
iOS SDKを初期化し開始する方法を説明します。
始める前に
- 実装の前に、SDKをインストールする必要があります。
- このドキュメントには実装の例が含まれています。次の部分を置き換えて実装してください。
<AF_DEV_KEY>
:AppsFlyer Devキー。<APPLE_APP_ID>
:Apple App ID(id
プレフィックスを含まない)。- 必要に応じて、追加のプレースホルダー。
iOS SDKの初期化
ステップ1:依存関係のインポート
Import AppsFlyerLib
:
// AppDelegate.h
#import <AppsFlyerLib/AppsFlyerLib.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@end
import UIKit
import AppsFlyerLib
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
// ...
}
ステップ2:SDKの初期化
didFinishLaunchingWithOptions
で、Apple App IDとAppsFlyer Devキーを設定してください:
[[AppsFlyerLib shared] setAppsFlyerDevKey:@"<AF_DEV_KEY>"];
[[AppsFlyerLib shared] setAppleAppID:@"<APPLE_APP_ID>"];
AppsFlyerLib.shared().appsFlyerDevKey = "<AF_DEV_KEY>"
AppsFlyerLib.shared().appleAppID = "<APPLE_APP_ID>"
iOS SDKの開始
applicationDidBecomeActive
で、start
を呼び出してください:
[[AppsFlyerLib shared] start];
func applicationDidBecomeActive(_ application: UIApplication) {
AppsFlyerLib.shared().start()
// ...
}
Add SceneDelegate support
オプションSceneDelegate
を使用する場合のみ次の手順を実行してください:
didFinishLaunchingWithOptions
で、UIApplicationDidBecomeActiveNotification
オブザーバーを追加し、start
を実行するように設定してください:
@implementation AppDelegate
// SceneDelegate support - start AppsFlyer SDK
- (void)sendLaunch:(UIApplication *)application {
[[AppsFlyerLib shared] start];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// ...
// SceneDelegate support
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(sendLaunch:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
// ...
return YES;
}
// ...
@end
import UIKit
import AppsFlyerLib
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
...
// SceneDelegate support
NotificationCenter.default.addObserver(self, selector: NSSelectorFromString("sendLaunch"), name: UIApplicationdidBecomeActiveNotification, object: nil)
return true
}
// SceneDelegate support - start AppsFlyer SDK
@objc func sendLaunch() {
AppsFlyerLib.shared().start()
}
// ...
}
Start with completion handler
オプション
SDKが正常に開始されAppsFlyerサーバーに通知されたのを確認するために、完了ハンドラと一緒に start
を呼び出してください。その後、SDK起動の成功または失敗を処理するロジックを適用できます。
[[AppsFlyerLib shared] startWithCompletionHandler:^(NSDictionary<NSString *,id> *dictionary, NSError *error) {
if (error) {
NSLog(@"%@", error);
return;
}
if (dictionary) {
NSLog(@"%@", dictionary);
return;
}
}];
AppsFlyerLib.shared()?.start(completionHandler: { (dictionary, error) in
if (error != nil){
print(error ?? "")
return
} else {
print(dictionary ?? "")
return
}
})
例
#import "AppDelegate.h"
#import <AppsFlyerLib/AppsFlyerLib.h>
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate ()
@end
@implementation AppDelegate
// Start the AppsFlyer SDK
- (void)sendLaunch:(UIApplication *)application {
[[AppsFlyerLib shared] start];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
/** APPSFLYER INIT **/
[AppsFlyerLib shared].appsFlyerDevKey = @"<AF_DEV_KEY>";
[AppsFlyerLib shared].appleAppID = @"<APPLE_APP_ID>";
/* Uncomment the following line to see AppsFlyer debug logs */
// [AppsFlyerLib shared].isDebug = true;
// SceneDelegate support
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(sendLaunch:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
if (@available(iOS 10, *)) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];
}
else {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
[[UIApplication sharedApplication] registerForRemoteNotifications];
return YES;
}
@end
import UIKit
import AppsFlyerLib
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, AppsFlyerLibDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
AppsFlyerLib.shared().appsFlyerDevKey = "<AF_DEV_KEY>"
AppsFlyerLib.shared().appleAppID = "<APPLE_APP_ID>"
/* Uncomment the following line to see AppsFlyer debug logs */
// AppsFlyerLib.shared().isDebug = true
// SceneDelegate support
NotificationCenter.default.addObserver(self, selector: NSSelectorFromString("sendLaunch"), name: UIApplication.didBecomeActiveNotification, object: nil)
return true
}
// SceneDelegate support
@objc func sendLaunch() {
AppsFlyerLib.shared().start()
}
// ...
}
iOS 14サポート
iOS 14以降の機能をサポートするための設定方法は以下の通りです。
Enabling App Tracking Transparency (ATT) support
iOS 14.5以降、IDFAへのアクセスはATTフレームワークによって管理されます。
SDKでATTサポートを有効化することで、iOS 14.5
以降がインストールされているデバイスでのIDFA収集を処理します。
注記
アプリ内のどこかで
requestTrackingAuthorization
を呼び出す予定がある場合にのみ、waitForATTUserAuthorization
を呼び出してください。
ステップ1:セットアップ waitForATTUserAuthorization
SDKを初期化する際に、didFinishLaunchingWithOptions
で start
を呼び出す前に、waitForATTUserAuthorization
を呼び出してください。
[[AppsFlyerLib shared] waitForATTUserAuthorizationWithTimeoutInterval:60];
AppsFlyerLib.shared().waitForATTUserAuthorization(timeoutInterval: 60)
ATTの同意ダイアログを表示してからユーザーが操作するまでに十分な時間が確保できるように、timeoutInterval
を設定してください。例:
- アプリの初回起動時にATTのポップアップが表示される場合には、60秒程度の設定で十分でしょう。
- チュートリアル完了の後にATTのポップアップが表示され、チュートリアルの完了までに約 2 分かかるのであれば、180秒程度の設定で十分でしょう。
ステップ2:呼び出し requestTrackingAuthorization
プロンプトを表示したい場所でrequestTrackingAuthorization
を呼び出してください。
- (void)didBecomeActiveNotification {
// start is usually called here:
// [[AppsFlyerLib shared] start];
if @available(iOS 14, *) {
[ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
NSLog(@"Status: %lu", (unsigned long)status);
}];
}
}
@objc func didBecomeActiveNotification() {
// start is usually called here:
// AppsFlyerLib.shared().start()
if #available(iOS 14, *) {
ATTrackingManager.requestTrackingAuthorization { (status) in
switch status {
case .denied:
print("AuthorizationSatus is denied")
case .notDetermined:
print("AuthorizationSatus is notDetermined")
case .restricted:
print("AuthorizationSatus is restricted")
case .authorized:
print("AuthorizationSatus is authorized")
@unknown default:
fatalError("Invalid authorization status")
}
}
}
}
注意
requestTrackingAuthorization
を呼び出すためには、AppTrackingTransparency
フレームワークをインポートする必要があります。- Appleのドキュメントによると:
requestTrackingAuthorization
は、アプリがUIApplicationStateActive
の状態にある場合のみに起動されます。requestTrackingAuthorization
は App Extenions からは呼び出すことはできません。
Customizing the ATT consent dialog
ATT同意ダイアログは、Xcodeプロジェクトのinfo.plist
を変更することでカスタマイズできます。
詳しい説明は、Appleのドキュメントを参照してください。
Attributing App Clips
Apple App Clips計測は iOS SDKV6.0.8
で導入されました。詳しい説明はApp Clips実装ガイドを参照してください。
Sending SKAN postback copies to AppsFlyer
iOS 15
ポストバックのコピーをAppsFlyerに送信するようにアプリを設定してください。
AppsFlyerのエンドポイントを登録する方法:
NSAdvertisingAttributionReportEndpoint
キーをアプリのinfo.plist
に追加してください。- キーの値を
https://appsflyer-skadnetwork.com/
に設定してください。
Appleによると、1つのエンドポイントのみを設定できます。受信したポストバックのコピーはポストバックコピーレポートにて確認できます。
デバッグモードの有効化
デバッグログを有効にするには、isDebug を true
に設定してください:
[AppsFlyerLib shared].isDebug = true;
AppsFlyerLib.shared().isDebug = true
注意
完全なデバッグログを見るためには、他のSDKメソッドを呼び出す前に必ず
isDebug
を設定してください。例を参照してください。
警告
機密情報の漏洩を防ぐため、アプリを配布する前にデバッグログが無効になっていることを確認してください。
連携のテスト
連携テストの詳細な手順は、iOS SDK連携テストガイドを参照してください。
更新済 2か月前