Connecting

Connecting the Layer Client to Layer’s servers allows authentication to procede and data to flow.

How to do it

// 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];

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

Note

Make sure you attempt to connect the Layer client before doing anything related to Layer. If your app is primarily centered around messaging, a good place to put this code might be in application:didFinishLaunchingWithOptions:.

Disconnecting

The Layer client may become disconnected for three reasons:

  • You haven’t called the code to connect it yet
  • There’s no network access (note that it may look like the device is connected to the internet, but Layer may be blocked by a firewall, proxy, or other configuration setting)
  • You successfully deauthenticate a user

You can check if the client is connected, and reconnect as needed:

if (!self.layerClient.isConnected) {
  [self.layerClient connectWithCompletion:^(BOOL success, NSError *error) {
    // ...
  }];
}

Note that if the client fails to connect the first time, we will automatically retry a few times before giving up.

Best practice

We recommend putting the connection code in exactly one place in your app — this may be when your app loads, or where your Client is initialized.

Setup Application Authentication