This guide shows how to add AppsFlyer's SDK your iOS app. You can track installs, updates, sessions and additional in-app events beyond app installs (including in-app purchases, game levels, etc.) to evaluate ROI and user engagement levels. The iOS SDK is compatible with all iOS devices (iPhone, iPod, iPad) with iOS version 6 and above.
Podfile
:
pod 'AppsFlyerFramework'
pod install
.xcworkspace
file to open your project in Xcode, instead of the .xcodeproj
file, from here on out.Cartfile:
github "AppsFlyerSDK/AppsFlyerFramework"
Note: This approach is only compatible with iOS 8 and above.
Note: This approach is only recommend if you need to support iOS 7 in your application.
AppsFlyer's SDK uses the following native frameworks:
didFinishLaunchingWithOptions
method with your app ID taken from iTunes Connect and your AppsFlyer dev key:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Replace MY_APPSFLYER_KEY below by your AppsFlyer dev key
AppsFlyerTracker.shared().appsFlyerDevKey = "MY_APPSFLYER_KEY"
// MY_APP_ID below stands for you app ID on iTunes Connect
AppsFlyerTracker.shared().appleAppID = "1234567890"
return true
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Replace MY_APPSFLYER_KEY below by your AppsFlyer dev key
[AppsFlyerTracker sharedTracker].appsFlyerDevKey = @"MY_APPSFLYER_KEY";
// MY_APP_ID below stands for you app ID on iTunes Connect. Should be 9 or 10 digits.
[AppsFlyerTracker sharedTracker].appleAppID = @"MY_APP_ID";
// do any other necessary launch configuration
return YES;
}
applicationDidBecomeActive
function:
func applicationDidBecomeActive(application: UIApplication) {
// Track Installs, updates & sessions(app opens) (You must include this API to enable tracking)
AppsFlyerTracker.shared().trackAppLaunch()
// your other code here....
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Track Installs, updates & sessions(app opens) (You must include this API to enable tracking)
[[AppsFlyerTracker sharedTracker] trackAppLaunch];
// your other code here....
}
We highly recommend to have deep linking integrated in your app.
iOS9 and above requires your app to support Universal Links. See AppsFlyer's Universal Links integration guide for details.
To report such launches, add the following code to the app delegate:
// Reports app open from a Universal Link for iOS 9 or later
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
AppsFlyerTracker.shared().continue(userActivity, restorationHandler: restorationHandler)
return true
}
// Reports app open from deeplink from apps which do not support Universal Links (Twitter) and for iOS8 and below
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
AppsFlyerTracker.shared().handleOpen(url, sourceApplication: sourceApplication, withAnnotation: annotation)
return true
}
// Reports app open from deeplink for iOS 10 or later
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
AppsFlyerTracker.shared().handleOpen(url, options: options)
return true
}
// Reports app open from a Universal Link for iOS 9 or above
- (BOOL) application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *_Nullable))restorationHandler {
[[AppsFlyerTracker sharedTracker] continueUserActivity:userActivity restorationHandler:restorationHandler];
return YES;
}
// Reports app open from deeplink from apps which do not support Universal Links (Twitter) and for iOS8 and below
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation {
[[AppsFlyerTracker sharedTracker] handleOpenURL:url sourceApplication:sourceApplication withAnnotation:annotation];
return YES;
}
// Reports app open from deeplink for iOS 10
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
options:(NSDictionary *) options {
[[AppsFlyerTracker sharedTracker] handleOpenURL:url options:options];
return YES;
}
In App Events provide insight on what is happening in your app. It is recommended to take the time and define the events you would like to measure to allow you to track ROI (Return on Investment) and LTV (Lifetime Value).
Tracking in app events is done by calling trackEvent
with event name and value parameters. See In App Evnets documentation for more details
An In-app event name must be no longer than 45 characters. Events names with more than 45 characters do not appear in the dashboard, but only in the raw Data, Pull and Push APIs.
AppsFlyerTracker.shared().trackEvent(AFEventLevelAchieved, withValues: [
AFEventParamLevel: 9,
AFEventParamScore : 100
]);
[[AppsFlyerTracker sharedTracker] trackEvent: AFEventLevelAchieved withValues:@{
AFEventParamLevel: @9,
AFEventParamScore : @100
}];
This generates an event of type “af_level_achieved” (using the AFEventLevelAchieved
constant) with the following event values:
{af_level: 9 , af_score: 100}
Use the AFEventParamRevenue
event parameter to count revenue as part of an in-app event. You can populate it with any numeric value, positive or negative.
NOTE AFEventParamPrice
(equals to using "af_price") is not counted as revenue and is a descriptive parameter which does not affect revenue and LTV measurements.
AppsFlyerTracker.shared().trackEvent(AFEventLevelAchieved, withValues: [
AFEventParamContentId:"1234567",
AFEventParamContentType : "category_a",
AFEventParamRevenue: 1.99,
AFEventParamCurrency:"USD"
]);
[[AppsFlyerTracker sharedTracker] trackEvent: AFEventLevelAchieved withValues:@{
AFEventParamContentId:@"1234567",
AFEventParamContentType : @"category_a",
AFEventParamRevenue: @1.99,
AFEventParamCurrency:@"USD"
}];
AppsFlyer allows you to access the user attribution data in real time directly at the SDK level. It enables you to customize the landing page a user sees on the very first app open after a fresh app install. For more information regarding this advanced functionality, read here.
AppsFlyer unique user ID is created for every new install of an app. You can use get it with the following code:
let appsflyerId = AppsFlyerTracker.shared().getAppsFlyerUID()
NSString *appsflyerId = [AppsFlyerTracker sharedTracker].appleAppID;
If your app has its own user ID that you want to track users with, you can use customerUserID
:
AppsFlyerTracker.shared().customerUserID = "my user id"
[AppsFlyerTracker sharedTracker].customerUserID = @"my user id";
IMPORTANT NOTES:
The customerUserID
, must be set before the trackAppLaunch.
It is recommended to set your Customer User ID as soon as possible as it is only associated to events reported after setting this attribute.
You must set the Customer User ID using this API in order to use AppsFlyer’s integrations with Analytics platforms such as Mixpanel and Swrve.
For more information about the Customer User ID, click here.
AppsFlyer enables you to report one or more of the user’s email addresses if you collect them in your app. The email values can be encrypted by following encryption methods: Sha1, MD5 and plain.
AppsFlyerTracker.shared().setUserEmails( ["email1@domain.com", "email2@domain.com"], with: EmailCryptTypeSHA1)
[[AppsFlyerTracker sharedTracker] setUserEmails:@[@"email1@domain.com", @"email2@domain.com"] withCryptType:EmailCryptTypeSHA1];
NOTE: Personally Identifiable Information (PII) such as email addresses are not retained by AppsFlyer and this information is not presented in any reports. The purpose of collecting this information is solely for postback purposes to the media source.
To stop all installs, events and sessions tracking for a user, use deviceTrackingDisabled
:
AppsFlyerTracker.shared().deviceTrackingDisabled = true
[AppsFlyerTracker sharedTracker].deviceTrackingDisabled = YES;
Tracking can be restarted by calling deviceTrackingDisabled
again setting to false.
Uninstalls tracking requires you to enable remote push notification on your app. See Apple's Remote Notification Programming Guide for more details.
Add the following code to your AppDelegate.m:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// The userNotificationTypes below is just an example and may be changed depending on the app
let pushSettings: UIRemoteNotificationSettings =
UIRemote NotificationSettings(forTypes:
UIUserNotificationType.Alert |
UIUserNotificationType.Badge |
UIUserNotificationType.Sound,
categories: nil)
UIApplication.sharedApplication().registerForRemoteNotificationSettings(pushSettings)
UIApplication.sharedApplication().registerForRemoteNotifications()
// if you do not use push notificaiton in your app, uncomment the following line
//UIApplication.sharedApplication().applicationIconBadgeNumber = 0
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
AppsFlyerTracker.shared().registerUninstall(deviceToken)
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// The userNotificationTypes below is just an example and may be changed depending on the app
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
// if you do not use push notificaiton in your app, uncomment the following line
//application.applicationIconBadgeNumber = 0;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[[AppsFlyerTracker sharedTracker] registerUninstall:deviceToken];
}
To enable tracking app launches from push notifications add the following code to your app delegate:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
AppsFlyerTracker.shared().handlePushNotification(userInfo)
}
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[[AppsFlyerTracker sharedTracker] handlePushNotification:userInfo];
}
The push message should have an "af" parameter with AppsFlyer tracking params.
{
"aps": {
"alert": "Push text",
"sound": "default",
"category": "REMINDER_CATEGORY"
},
"_p": 123456,
"payloadKey": "payloadValue"
"af": {
"pid": "swrve_int",
"is_retargeting": true,
"c": "test_campaign"
}
}
The app extension requires permissions to use the Internet:
Add the following code to the app extension's UIViewController viewDidLoad
:
override func viewDidLoad() {
super.viewDidLoad()
AppsFlyerTracker.shared().appsFlyerDevKey = "MY_APPSFLYER_KEY"
// MY_APP_ID below stands for you app ID on iTunes Connect. Should be 9 or 10 digits.
AppsFlyerTracker.shared().appleAppID = "MY_APP_ID"
AppsFlyerTracker.sharedTracker().trackAppLaunch()
}
- (void)viewDidLoad {
[super viewDidLoad];
// Replace MY_APPSFLYER_KEY below by your AppsFlyer dev key
[AppsFlyerTracker sharedTracker].appsFlyerDevKey = @"MY_APPSFLYER_KEY";
// MY_APP_ID below stands for you app ID on iTunes Connect. Should be 9 or 10 digits.
[AppsFlyerTracker sharedTracker].appleAppID = @"MY_APP_ID";
[[AppsFlyerTracker sharedTracker] trackAppLaunch];
}
To receive attribution data on the app extension, follow the instruction on Attribution Data and implement it on the UIViewController of your app instead of in the AppDelegate.
You can test your integration with the SDK Integration Test page on AppsFlyer's dashboard.
You can find the submission instructions here