Manual integration

Follow these steps to manually integrate Localytics into your iOS app.

  1. In your application's delegate file, the file whose default name is ‘<YourApplication>AppDelegate.m’, add the following line under any existing imports.
    #import "Localytics.h"
    
  2. In the same file, add the following lines to the start of didFinishLaunchingWithOptions. This configures the Localytics library when your application is loaded.
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
       [Localytics integrate:@"App Key"];
    
       if (application.applicationState != UIApplicationStateBackground)
       {
           [Localytics openSession];
       }
  3. Include the following code to register for push notifications.
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
       {
           UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
           UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
           [application registerUserNotificationSettings:settings];
           [application registerForRemoteNotifications];
       }
       else
       {
           [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
       }
    
       return YES;
    }
  4. If your app delegate does not implement application:didReceiveRemoteNotification:fetchCompletionHandler:, include the following code at the end of didFinishLaunchingWithOptions to handle opened push notifications.
    if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey])
    {
        [Localytics handlePushNotificationOpened:[launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
    }
  5. Add the following lines to the start of applicationDidBecomeActive and applicationWillEnterForeground. This code opens or resumes a session and attempts an upload when your application is loaded or foregrounded.
    - (void)applicationDidBecomeActive:(UIApplication *)application
    {
        [Localytics openSession];
        [Localytics upload];
    }
    
    - (void)applicationWillEnterForeground:(UIApplication *)application
    {
        [Localytics openSession];
        [Localytics upload];
    }
  6. Add the following lines to the start of applicationWillResignActive and applicationDidEnterBackground. This code dismisses any active in-app messaging campaigns, closes the current session, and attempts an upload when your application is backgrounded.
    - (void)applicationWillResignActive:(UIApplication *)application
    {
        [Localytics dismissCurrentInAppMessage];
        [Localytics closeSession];
        [Localytics upload];
    }
    
    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        [Localytics dismissCurrentInAppMessage];
        [Localytics closeSession];
        [Localytics upload];
    }
  7. Add the following code for push registration.
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
        [Localytics setPushToken:deviceToken];
    }
     
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
    {
        NSLog(@"Failed to register for remote notifications: %@", [error description]);
    }
     
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
    {
        [Localytics handlePushNotificationOpened:userInfo];
        completionHandler(UIBackgroundFetchResultNoData);
    }
  8. Add the following code for in-app messaging test mode.
    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    {
        return [Localytics handleTestModeURL:url];
    }
  9. Test your app. Run your application in the simulator (or on a real device if possible) and check your Localytics Dashboard to see the data being reported. Remember that the upload is only guaranteed when the session is opened and closed, so any events you tag may not appear until your second session.
  10. Enable background push for your app.
Congratulations! You're integrated. Check out the data in your Dashboard or get even more insights when you start tagging events. Don't forget to test your app. Run it in an emulator, or on a real device if possible.