Mengintegrasikan SDK

Pelajari cara menginisialisasi dan memulai SDK iOS.

Sebelum Anda memulai:

  • Sebelum mengintegrasikan, Anda harus Menginstal SDK.
  • Dokumen ini berisi contoh implementasinya. Pastikan Anda mengganti hal berikut:
    • <YOUR_DEV_KEY>: Kode pengembang AppsFlyer.
    • <APPLE_APP_ID>: ID Aplikasi Apple (tanpa id awalan).
    • Placeholder tambahan, jika diperlukan.

Menginisialisasi SDK iOS

Langkah 1: Impor dependensi
Impor AppsFlyerLib:

// AppDelegate.h
#import <AppsFlyerLib/AppsFlyerLib.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@end
import UIKit
import AppsFlyerLib

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    // ...
}

Langkah 2: Menginisialisasi SDK
In didFinishLaunchingWithOptions konfigurasi ID Aplikasi Apple dan kode pengembang AppsFlyer:

[[AppsFlyerLib shared] setAppsFlyerDevKey:@"<YOUR_DEV_KEY>"];
[[AppsFlyerLib shared] setAppleAppID:@"<APPLE_APP_ID>"];
AppsFlyerLib.shared().appsFlyerDevKey = "<YOUR_DEV_KEY>"
AppsFlyerLib.shared().appleAppID = "<APPLE_APP_ID>"

Memulai SDK iOS

In applicationDidBecomeActive, call start:

[[AppsFlyerLib shared] start];
func applicationDidBecomeActive(_ application: UIApplication) {
    AppsFlyerLib.shared().start()
    // ...
}

Add SceneDelegate support

OPSIONAL
Lakukan hal ini jika Anda menggunakan SceneDelegates:

In didFinishLaunchingWithOptions, tambahkan UIApplicationDidBecomeActiveNotification pengamat dan atur untuk lari 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

OPSIONAL
Untuk menginformasikan bahwa SDK dimulai secara sukses dan melaporkan server AppsFlyer, panggil start dengan handler penyelesaian. Kemudian Anda dapat menerapkan logika untuk menangani keberhasilan atau kegagalan peluncuran 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
            }
        })

Contoh lengkap

#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 = @"<YOUR_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 {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        AppsFlyerLib.shared().appsFlyerDevKey = "<YOUR_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()
    }
// ...
}

Tautan Github

Setting the Customer User ID

OPSIONAL
The Customer User ID (CUID) is a unique user identifier created outside the SDK by the app owner. If made available to the SDK, it can be associated with installs and other in-app events. These CUID-tagged events can be cross-referenced with user data from other devices and applications.

Set the CUID

To set the CUID:

[AppsFlyerLib shared].customerUserID = @"my user id";
AppsFlyerLib.shared().customerUserID = "my user id"

📘

Catatan

The Customer User ID must be set with every app launch.

Associate the CUID with the install event

If it’s important for you to associate the install event with the CUID, you should set to set the customerUserId before calling the start method. This is because start sends the install event to AppsFlyer. If the CUID is set after calling start, it will not be associated with the install event.

- (void)applicationDidBecomeActive:(UIApplication *)application {
  	// Your custom logic of retrieving CUID
    NSString *customUserId = [[NSUserDefaults standardUserDefaults] stringForKey:@"customerUserId"];  
    if (customUserId != nil && ![customUserId  isEqual: @""]) {
        // Set CUID in AppsFlyer SDK for this session
        [AppsFlyerLib shared].customerUserID = customUserId; 
        // Start
        [[AppsFlyerLib shared] start]; 
    }
}
func applicationDidBecomeActive(_ application: UIApplication) {
  //  your logic to retrieve CUID
  let customUserId = UserDefaults.standard.string(forKey: "customUserId") 
  
  if(customUserId != nil && customUserId != ""){
     // Set CUID in AppsFlyer SDK for this session
    AppsFlyerLib.shared().customerUserID = customUserId    
    AppsFlyerLib.shared().start() // Start
  }
}

Log sessions

The SDK sends an af_app_opened message whenever the app is opened or brought to the foreground, providing that start is called in the didBecomeActive lifecycle event method. Before the message is sent, the SDK makes sure that the time passed since sending the last message is not smaller than a predefined interval.

Setting the time interval between app launches

Atur minTimeBetweenSessions to the minimal time interval that must lapse between two af_app_opened messages. The default interval is 5 seconds.

Dukungan iOS 14

Berikut adalah panduan untuk mengatur dukungan untuk fitur iOS 14+.

Enabling App Tracking Transparency (ATT) support

Mulai iOS 14.5, Akses IDFA diatur oleh framework ATT.
Pengaktifan dukungan ATT di SDK menangani koleksi IDFA di perangkat dengan iOS 14.5+ diinstal

🚧

Perhatikan

Panggil waitForATTUserAuthorization hanya jika Anda ingin memanggil requestTrackingAuthorization di suatu tempat di aplikasi Anda.

Langkah 1: Atur waitForATTUserAuthorization
Ketika Menginisialisasi SDK, sebelum memanggil start In applicationDidBecomeActive, call waitForATTUserAuthorization:

[[AppsFlyerLib shared] waitForATTUserAuthorizationWithTimeoutInterval:60];
AppsFlyerLib.shared().waitForATTUserAuthorization(timeoutInterval: 60)

Tautan Github

Atur timeoutInterval sehingga pengguna aplikasi memiliki cukup waktu untuk melihat dan berinteraksi dengan prompt ATT. Berikut beberapa contohnya:

  • Jika prompt ATT ditampilkan pada peluncuran aplikasi–interval 60 detik sudah cukup
  • Jika prompt ATT ditampilkan setelah tutorial yang membutuhkan waktu sekitar 2 menit untuk diselesaikan–interval 120 detik sudah cukup.

Langkah 2: Panggil metode requestTrackingAuthorization
Panggil requestTrackingAuthorization di mana Anda ingin menampilkan prompt:

- (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")
        }
      }
    }
}

Tautan Github

📘

Catatan

  • Anda butuh mengimpor AppTrackingTransparency kerangka kerja untuk memanggil requestTrackingAuthorization.
  • Berdasarkan dokumentasi Apple:
    • requestTrackingAuthorization dipanggil hanya jika aplikasi sedang dalam UIApplicationStateActive keadaan.
    • requestTrackingAuthorization tidak dapat dipanggil dari Ekstensi Aplikasi.

Customizing the ATT consent dialog

Dialog persetujuan ATT dapat disesuaikan dengan mengubah proyek Xcode Anda info.plist:

Untuk rincian petunjuknya, lihat Dokumentasi Apple.

Attributing App Clips

Atribusi Apple App Clips diperkenalkan di iOS SDK. V6.0.8. Lihat Panduan integrasi App Clips untuk instruksi yang lebih detail.

Sending SKAN postback copies to AppsFlyer

iOS 15
Konfigurasikan aplikasi Anda untuk mengirim salinan postback ke AppsFlyer.

Untuk mendaftarkan titik akhir AppsFlyer:

  1. Tambahkan NSAdvertisingAttributionReportEndpoint kode untuk aplikasi Anda info.plist.
  2. Atur nilai kode ke https://appsflyer-skadnetwork.com/.

Menurut Apple, Anda hanya dapat mengatur satu titik akhir. Salinan postback yang diterima tersedia di laporan salinan postback.

mengaktifkan mode debug

Anda dapat mengaktifkan log debug dengan mengatur isDebug ke true:

[AppsFlyerLib shared].isDebug = true;
AppsFlyerLib.shared().isDebug = true

📘

Catatan

Untuk melihat log debug yang lengkap, pastikan untuk mengatur isDebug sebelum memanggil metode SDK lainnya.

Lihat contoh.

🚧

peringatan

Untuk menghindari kebocoran informasi sensitif, pastikan log debug dinonaktifkan sebelum mendistribusikan aplikasi.

Menguji integrasi

Untuk rincian petunjuk pengujian integrasi, lihat panduan pengujian integrasi SDK iOS.