Setup Application

Before getting started with learning the core concepts of Layer, it will be helpful to setup a few important pieces of code in your application. If your app is primarily focused around messaging then most of these methods will probably be needed. Add them to your application now, then read about their functionality in their respective sections.

Layer Client

Initialize a single instance of LYRClient to use throughout your app. You can create this in either your app delegate or a dedicated LayerController class, for example. Make this class implement the LYRClientDelegate protocol.

@interface AppDelegate () <LYRClientDelegate>
// ...

// Initializes a LYRClient object
NSURL *appID = [NSURL URLWithString:@"layer:///apps/staging/cf10048c-d9ab-11e5-b6a9-c01d00006542"];
self.layerClient = [LYRClient clientWithAppID:appID delegate:self options:nil];

Connecting

Wherever you are initializing your Layer Client, create a new method to connect it with the Layer service and include this code:

// Tells LYRClient to establish a connection with the Layer service
[self.layerClient connectWithCompletion:^(BOOL success, NSError *error) {
    if (success) {
        NSLog(@"Client is Connected!");
    }
}];

Authentication

Create a new method for authenticating with Layer and include this code. Put this in exactly one place in your app. This could be in your app delegate if your app is purely messaging, or in a LayerController class or something similar.

/*
 * 1. Request nonce from Layer.  Each nonce is valid for 10 minutes after
 *    creation, after which you will have to generate a new one.
 */
[layerClient requestAuthenticationNonceWithCompletion:^(NSString *nonce, NSError *error) {

   /*
    * 2. Connect to your backend to generate an identity token.
    */
    NSString *identityToken = ...

    /*
     * 3. Submit identity token to Layer for validation
     */
     [layerClient authenticateWithIdentityToken:identityToken completion:^(LYRIdentity *authenticatedUser, NSError *error) {
         if (authenticatedUser) {
             NSLog(@"Authenticated as User: %@", authenticatedUser);
         }
     }];
}];

Authentication Challenge Handling

Include this method in your LYRClientDelegate class and implement the same authentication flow as before. This will ensure the user automatically re-authenticates whenever their session expires.

- (void)layerClient:(LYRClient *)client didReceiveAuthenticationChallengeWithNonce:(NSString *)nonce
{
    NSLog(@"Layer Client did receive an authentication challenge with nonce=%@", nonce);

   /*
    * 1. Connect to your backend to generate an identity token using the provided nonce.
    */
    NSString *identityToken = ...

   /*
    * 2. Submit identity token to Layer for validation
    */
    [layerClient authenticateWithIdentityToken:identityToken completion:^(LYRIdentity *authenticatedUser, NSError *error) {
        if (authenticatedUser) {
            NSLog(@"Authenticated as User: %@", authenticatedUser);
        }
    }];
}

Push Notifications

  1. Include this code within application:didFinishLaunchingWithOptions:. This will register the app to receive push notifications.
// Checking if app is running iOS 8
if ([application respondsToSelector:@selector(registerForRemoteNotifications)]) {
  // Register device for iOS8
  UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
  [application registerUserNotificationSettings:notificationSettings];
  [application registerForRemoteNotifications];
} else {
  // Register device for iOS7
  [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
}
  1. Include this code within application:didRegisterForRemoteNotificationsWithDeviceToken:. This will register the device to receive push notifications. Use the supplied deviceToken each time and do not cache it.
NSError *error;
BOOL success = [layerClient updateRemoteNotificationDeviceToken:deviceToken error:&error];
if (success) {
  NSLog(@"Application did register for remote notifications");
} else {
  NSLog(@"Error updating Layer device token for push:%@", error);
}
  1. Include this code within application:didReceiveRemoteNotification:. When tapping on a push notification, this will synchronize the relevant conversation to ensure it’s available to use as quickly as possible.
BOOL success = [layerClient synchronizeWithRemoteNotification:userInfo completion:^(LYRConversation * _Nullable conversation, LYRMessage * _Nullable message, NSError * _Nullable error) {
  if (conversation) {
      // Handle synced conversation. For example, you could navigate to that conversation view.
  }
}];
if (success) {
  NSLog(@"Application did synchronize with remote notification");
} else {
  NSLog(@"Error synchronizing with remote notification:%@", error);
}
Install XDK Connecting