Suggestions

close search

Add Messaging, Voice, and Authentication to your apps with Vonage Communications APIs

Visit the Vonage API Developer Portal

Joining a Session — iOS (Swift)

Using a session ID and token, you can connect to the OpenTok session using the OpenTok iOS SDK.

This topic includes the following sections:

For information on creating a session ID and an authentication token, see Session Creation and Token Creation.

Initializing an OTSession object

Before you can connect to a session, instantiate an OTSession object by calling the OTSession init(apiKey:sessionId:delegate:) method with your OpenTok API key and the appropriate session ID:

// Replace kApiKey with your OpenTok API key:
// Replace kSessionId with an OpenTok session ID:
session = OTSession(apiKey: kApiKey, sessionId: kSessionId, delegate: self)

Note that calling the OTSession init(apiKey: sessionId: delegate:) method does not create an OpenTok session; it creates the Swift OTSession object, which represents an existing OpenTok session. You create an OpenTok session using the OpenTok server-side libraries. See Creating an OpenTok session.

Implement the methods of the OTSessionDelegate protocol in the object you specify as the delegate object. These methods are called when session-related events occur.

Connecting to a session

Call the OTSession connect(withToken:error:) method, passing in the token for the client:

var error: OTError?
session.connect(withToken: token, error: &error)

if let error = error {
	print("connect failed with error: \(error)")
}

The OTSessionDelegate sessionDidConnect(_:) message is sent when the client connects to the OpenTok session.

The OTSessionDelegate session(_:didFailWithError:) is sent when there is an error in connecting. See the documentation for the OTSessionErrorCode enum for descriptions of values of the code property of the error object.

Monitoring the connection status

You can get the connection status by checking the sessionConnectionStatus property of the OTSession object:

session.sessionConnectionStatus

Valid values are defined in the OTSessionConnectionStatus enum.

You can use a key-value observer to monitor this property. However, the OTSessionDelegate sessionDidConnect(_:) and OTSessionDelegate sessionDidDisconnect(_:) messages are sent to the session's delegate when the session connects and disconnects.

Disconnecting from a session

To disconnect from a session, call the OTSession disconnect(_:) method.

var error: OTError?
session.disconnect(&error)

if let error = error {
	print("disconnect failed with error: \(error)")
}

Detecting when you have disconnected

The OTSessionDelegate sessionDidDisconnect(_:) message is sent to the session's delegate when the session disconnects.

Note that sessions automatically disconnect when the app is suspended.

If the connection to the session drops due to an error that occurs after a successful connection, the OTSessionDelegate session(_:didFailWithError:) message is sent prior to the OTSessionDelegate sessionDidDisconnect(_:) message. The OTSessionErrorCode enum defines the code property of the OTError object passed into the OTSessionDelegate session(_:didFailWithError:) message, and it describes the the reason for the disconnection.

Automatic reconnection

Clients will attempt to automatically reconnect to a session they disconnect unexpectedly (for example, due to a drop in network connectivity). You do not need to add any code to have the clients reconnect automatically, unless you want to respond to messages sent when your client disconnects and reconnects. For sample code that demonstrates the use of these events, see the opentok-reconnection repo on GitHub.

When the connection is dropped and the client tries to reconnect, the OTSessionDelegate sessionDidBeginReconnecting(_:) message is sent to the OTSession object's delegate. When the connection is restored, the OTSessionDelegate sessionDidReconnect(_:) message is sent. If the client cannot restore the connection, the OTSessionDelegate sessionDidDisconnect(_:) message is sent.

In response to these events, your application can (optionally) display user interface notifications indicating the temporary disconnection, reconnection, and disconnection states.

// OTSession delegate callbacks:
func sessionDidBeginReconnecting(_ session:OTSession) {
	// Display a user interface notification.
}

func sessionDidReconnect(_ session: OTSession) {
	// Adjust user interface.
}

func sessionDidDisconnect(_ session: OTSession) {
	// Adjust user interface.
}

When your client temporarily disconnects from a session, the OKSubscriberKitDelegate objects in clients subscribing to a stream you publish send messages when your published stream drops and when (and if) it resumes automatically. For more information, see Automatic reconnection in the "Subscribing to streams" developer guide.

By default, any signals you send while your client is temporarily disconnected from a session are queued and sent when (and if) you successfully reconnect. You can use the OTSession signal(withType: string:connection:retryAfterReconnect:error:) method to prevent signals from being queued while you are disconnected. For more information, see Preventing signals from being sent during automatic reconnection.

Detecting when other clients have connected and disconnected

The OTSessionDelegate session(_: connectionCreated:) message is sent to the session's delegate when another client connects to the session (and for each client connected to the session when you connect).

The OTSessionDelegate session(_: connectionDestroyed:) message is sent to the session's delegate when another client disconnects from the session.

Running in the background state

See Using the OpenTok iOS SDK in applications running in the background state.