Setup

Install the SDK

The following setup guide uses Cocapods. If you prefer, you can install our SDK manually.

Supports iOS 7.0 and above.

Download CocoaPods

CocoaPods is the dependency manager for Objective-C and Swift projects. Install CocoaPods by running the following command:

$ sudo gem install cocoapods

For issues with installing CocoaPods, please refer here.

Add a Podfile

In the terminal, navigate to your app's directory. Add a podfile to your app by running the following command:

$ pod init

Edit your Podfile

Open your podfile by running the following command:

$ open -a Xcode Podfile

Insert the following into your Podfile:

pod 'Leanplum-iOS-SDK'

# Uncomment for UI Editor.
# pod 'Leanplum-iOS-UIEditor'

# Uncomment one of the following for Location Services.
# pod 'Leanplum-iOS-Location'
# pod 'Leanplum-iOS-LocationAndBeacons'

See Location Services for more info.

Install your pods

Now, install the Leanplum SDK pods by running the following command:

$ pod install

If you prefer to use Carthage, you can simply add the following to your Cartfile.

github "Leanplum/Leanplum-iOS-SDK"

# Uncomment for UI Editor. # github "Leanplum/Leanplum-iOS-UIEditor"

# Uncomment one of the following for Location Services. # github "Leanplum/Leanplum-iOS-Location" # github "Leanplum/Leanplum-iOS-LocationAndBeacons"

Add DEBUG flag

This is required for Swift only.

Add -D DEBUG to Other Swift Flags only for Debug mode.

Debug flag

Edit your AppDelegate

Remember to import Leanplum before doing anything. You can avoid this by adding the import to your app's prefix header (.pch) file.

#import "AppDelegate.h"
#import <Leanplum/Leanplum.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // Insert your API keys here.
  #ifdef DEBUG
    LEANPLUM_USE_ADVERTISING_ID;
    [Leanplum setAppId:@"YOUR_APP_ID"
     withDevelopmentKey:@"YOUR_DEVELOPMENT_KEY"];
  #else
    [Leanplum setAppId:@"YOUR_APP_ID"
     withProductionKey:@"YOUR_PRODUCTION_KEY"];
  #endif

  // Optional: Tracks in-app purchases automatically as the "Purchase" event.
  // To require valid receipts upon purchase or change your reported
  // currency code from USD, update your app settings.
  // [Leanplum trackInAppPurchases];

  // Optional: Tracks all screens in your app as states in Leanplum.
  // [Leanplum trackAllAppScreens];

  // Optional: Activates UI Editor.
  // Requires the Leanplum-iOS-UIEditor framework.
  // [[LeanplumUIEditor sharedEditor] allowInterfaceEditing];

  // Starts a new session and updates the app content from Leanplum.
  [Leanplum start];
  return YES;
}
...

@end
import UIKit
#if DEBUG
    import AdSupport
#endif
import Leanplum

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
  // We've inserted your Test API keys here for you :)
  #if DEBUG
    Leanplum.setDeviceId(ASIdentifierManager.shared().advertisingIdentifier.uuidString)
    Leanplum.setAppId("YOUR_APP_ID",
      withDevelopmentKey:"YOUR_DEVELOPMENT_KEY")
  #else
    Leanplum.setAppId("YOUR_APP_ID",
      withProductionKey: "YOUR_PRODUCTION_KEY")
  #endif

  // Optional: Tracks in-app purchases automatically as the "Purchase" event.
  // To require valid receipts upon purchase or change your reported
  // currency code from USD, update your app settings.
  // Leanplum.trackInAppPurchases()

  // Optional: Tracks all screens in your app as states in Leanplum.
  // Leanplum.trackAllAppScreens()

  // Optional: Activates UI Editor.
  // Requires the Leanplum-iOS-UIEditor framework.
  // LeanplumUIEditor.shared().allowInterfaceEditing()

  // Starts a new session and updates the app content from Leanplum.
  Leanplum.start()

  return true
}
...

Verify the setup

Run your project and register your device

Make sure you're in Debug mode. To ensure Debug mode is enabled, make sure the DEBUG preprocessor macro is set in Build Settings. From the Leanplum dashboard, go to Devices then hover over your device and click Register.

Debug mode

Test in-app messaging

Send yourself an in-app message to see it in action. While running your device in Debug mode, go to the Leanplum Message Composer. Create a new message and click Send Preview. The message will be sent to all registered test devices.

Create a custom variable (Optional)

Variables show up in our content management system. You can change them, segment them, and A/B test them without having to update your app. You can verify the variable has been set by viewing the Variables tab in the Leanplum dashboard.

Add the following lines to your app delegate or another class of your choosing:

#import <Leanplum/Leanplum.h>

DEFINE_VAR_STRING(welcomeMessage, @"Welcome to Leanplum!");

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ ...
  [Leanplum onVariablesChanged:^{
    NSLog(@"%@", welcomeMessage.stringValue);
  }];
  return YES;
}
...
@end
import UIKit
import Foundation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var welcomeMessage = LPVar.define("welcomeMessage",
    withString: "Welcome to Leanplum!")

    func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
        ...
        Leanplum.onVariablesChanged({
            NSLog((self.welcomeMessage?.stringValue())!)
        })
        ...
        return true
    }
...

It's important to use the variables changed callback onVariablesChanged, or welcomeMessage.onValueChanged (for our example variable welcomeMessage), if the value is needed around the time the app starts. This guarantees the latest value. See more on how to handle asynchronous code with callbacks.

Track an event (Optional)

Events allow you to measure statistics about what your users do inside your app. You can verify an event is tracked in the Debugger or Events tabs in the Dashboard.

Add the following lines of code to track an event. You can place it anywhere after calling start; this is just an example:

#import <Leanplum/Leanplum.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ ...
  [Leanplum onVariablesChanged:^{
    NSLog(@"%@", welcomeMessage.stringValue);
    [Leanplum track:@"Launch"];
  }];
  return YES;
}
...
@end
import UIKit
import Foundation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var welcomeMessage = LPVar.define("welcomeMessage", withString: "Welcome to Leanplum!")

    func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
        ...
        Leanplum.onVariablesChanged({
            NSLog((self.welcomeMessage?.stringValue())!)
            Leanplum.track("Launch")
        })
        ...
        return true
    }
...

Next

Users and Devices: Learn how to handle users, signing in, and user attributes.

Variables: Learn how to set up Leanplum variables for use in A/B tests.

Events: Learn how to track Events in Leanplum.

Messaging: Learn how to setup your app to receive Messages from Leanplum.

Callbacks: Learn how to handle the asynchronous nature of Leanplum in your app.