Integrate SDK

Learn how to initialize and start the iOS SDK.

Before you begin

  • Before integrating, you must Install the SDK.
  • This document contains example implementations. Make sure to replace the following:
    • <AF_DEV_KEY>: The AppsFlyer dev key
    • <APPLE_APP_ID>: The Apple App ID
    • Additional placeholders, where needed

Initializing the iOS SDK

Starting the SDK and getting attribution data in iOS

Step 1: Import dependencies
Import AppsFlyerLib and add AppsFlyerLibDelegate to AppDelegate:

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

@interface AppDelegate : UIResponder <UIApplicationDelegate, AppsFlyerLibDelegate>

@end
import UIKit
import AppsFlyerLib

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

Step 2: Initialize the SDK
In didFinishLaunchingWithOptions configure your Apple App ID and AppsFlyer dev key:

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

Starting the iOS SDK

In applicationDidBecomeActive, call start:

[[AppsFlyerLib shared] start];
AppsFlyerLib.shared().start()

Add SceneDelegate support

Optional
Do the following only if you use SceneDelegates:

In didFinishLaunchingWithOptions, add a UIApplicationDidBecomeActiveNotification observer and set it to run 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, AppsFlyerLibDelegate {
    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()
    }
// ...
}

Full example

#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;
  
    // Must be called AFTER setting appsFlyerDevKey and appleAppID
    [AppsFlyerLib shared].delegate = self;
    // 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
        // Must be called AFTER setting appsFlyerDevKey and appleAppID
        AppsFlyerLib.shared().delegate = self
        // 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 support

Following are guides on setting up support for iOS 14 features.

Enabling App Tracking Transparency (ATT) support

Starting iOS 14.5, IDFA access is governed by the ATT framework.
Enabling ATT support in the SDK handles IDFA collection on devices with iOS 14.5+ installed.

🚧

Attention

Call waitForATTUserAuthorization only if you intend to call requestTrackingAuthorization somewhere in your app.

Step 1: Set up waitForATTUserAuthorization
When Initializing the SDK, before calling start In didFinishLaunchingWithOptions, call waitForATTUserAuthorization:

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

Set timeoutInterval as such that app users have enough time to see and engage with the ATT prompt. A few examples:

  • If ATT prompt is displayed on app launch–a 60-second interval should be enough
  • If ATT prompt is displayed after a tutorial that takes approximately 2 minutes to complete–a 120-second interval should be enough.

Step 2: Call requestTrackingAuthorization
Call requestTrackingAuthorization where you wish to display the prompt:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if #available(iOS 14, *) {
      [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
        NSLog(@"Status: %lu", (unsigned long)status);
      }];
    }
}
override func viewDidLoad() {
  super.viewDidLoad()
  
  if #available(iOS 14, *) {
    ATTrackingManager.requestTrackingAuthorization { (status) in }
  }
}

📘

Note

You need to import the AppTrackingTransparency framework to call requestTrackingAuthorization.

Customizing the ATT consent dialog

The ATT consent dialog can be customized by modifying your Xcode project's info.plist:
customize-infoplist-att-stringcustomize-infoplist-att-string

For detailed instructions, see Apple's documentation.

Attributing App Clips

Apple App Clips attribution was introduced in iOS SDK V6.0.8. See our App Clips integration guide for detailed instructions.

Enabling debug mode

You can enable debug logs by setting isDebug to YES.

Testing the integration

For detailed integration testing instructions, see the testing article.

Updated 20 days ago



Integrate SDK


Learn how to initialize and start the iOS SDK.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.