Sample iOS application delegates

A sample automatic and manual application delegate.

Automatic

// AppDelegate.m
#import "AppDelegate.h"
#import "Localytics.h"

@implementation AppDelegate
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   [Localytics autoIntegrate:@"App Key" launchOptions:launchOptions];

   // If you are using Localytics Messaging 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;
}
  
// An action tied to a button
- (void)saveSettings:(id)sender 
{
   NSDictionary *dictionary = @{@"Use Location":@"Yes", @"Show Avatars":@"No"};
   [Localytics tagEvent:@"Settings Updated" attributes:dictionary];
}

@end

Manual

// AppDelegate.m
#import "AppDelegate.h"
#import "Localytics.h"

@implementation AppDelegate
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   [Localytics integrate:@"App Key"];

   if (application.applicationState != UIApplicationStateBackground)
   {
       [Localytics openSession];
   }
		
   // If you are using Localytics Messaging 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;
}
 
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [Localytics openSession];
    [Localytics upload];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [Localytics openSession];
    [Localytics upload];
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    [Localytics dismissCurrentInAppMessage];
    [Localytics closeSession];
    [Localytics upload];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [Localytics dismissCurrentInAppMessage];
    [Localytics closeSession];
    [Localytics upload];
}
 
- (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);
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [Localytics handleTestModeURL:url];
}

// An action tied to a button
- (void)saveSettings:(id)sender 
{
   NSDictionary *dictionary = @{@"Use Location":@"Yes", @"Show Avatars":@"No"};
   [Localytics tagEvent:@"Settings Updated" attributes:dictionary];
}

@end