close search

Hello World

This hello world procedure will get you the simplest possible iOS audio/video chat in 10 minutes. We'll use CocoaPods, Interface Builder, Objective-C, and the Xcode simulator.


Creating a new Xcode project

  1. Open Xcode and create a new project.
  2. Select Single View Application as the template.

Installing the OpenTok iOS SDK

  1. Create a Podfile in your project directory.
  2. Add pod 'OpenTok' to your Podfile.
  3. In a terminal prompt, navigate into your project directory and type pod install
  4. Reopen your project using the new *.xcworkspace file, as directed.

Importing the OpenTok.h file

Add the following to your ViewController.m file.

#import <OpenTok/OpenTok.h>

Implementing the OpenTok protocols

Within the ViewController.m file, declare three properties: session, publisher, and subscriber. The full code snippet follows.

@interface ViewController ()
<OTSessionDelegate, OTSubscriberDelegate, OTPublisherDelegate>
@property (strong, nonatomic) OTSession* session;
@property (strong, nonatomic) OTPublisher* publisher;
@property (strong, nonatomic) OTSubscriber* subscriber;

Getting an API key, session ID, and token

  1. Sign up for a TokBox Account.
    Free for thirty days. No credit card required.
  2. Use your credentials to log in to your TokBox Account.
  3. Create a project by clicking add project, typing a name, and clicking view project.
  4. Copy the API Key value and paste it into a text file.
  5. Within the Project Tools section, click Create Session ID.
  6. Copy the session ID from the box below the Create Session ID button and paste it into a text file.
  7. Within the Generate Token area, click Generate token.
    Make sure that Publisher is selected in the Role list box.
  8. Copy the token value from the box below the Generate token button and paste it into a text file.

Implementing the ViewController class

  1. Open the ViewController.m file
  2. To get you started quickly, we'll use static variables for the API key, session ID, and token values. Replace YOUR-API-KEY, YOUR-SESSION-ID, and YOUR-TOKEN with the values you obtained from your Account during the previous procedure.
    static NSString* const _apiKey = @"YOUR-API-KEY";
    static NSString* const _sessionId = @"YOUR-SESSION-ID";
    static NSString* const _token = @"YOUR-TOKEN";

    Important: You can continue to get the session ID and token values from the Project Page of your Account during testing and development. But before you go into production, you must deploy one of our server SDKs to generate these values dynamically.

  3. The application begins by initializing an OTSession instance using your API key and session ID. Then we can connect to the session using a token.
    - (void)viewDidLoad
        [super viewDidLoad];
        self.session = [[OTSession alloc] initWithApiKey:_apiKey
        [self.session connectWithToken:_token error:nil];
  4. Once the client succeeds in connecting to the session, initialize an OTPublisher object and publish a stream to the session. You can use the sessionDidConnect callback for this.
    - (void)sessionDidConnect:(OTSession*)session
        self.publisher = [[OTPublisher alloc]
        [session publish:self.publisher error:nil];
        [self.publisher.view setFrame:CGRectMake(10, 20, 175, 175)];
        [self.view addSubview:self.publisher.view];
  5. Add the OTSession delegate method to detect when a new stream gets created in the session and subscribe to it.
    - (void)session:(OTSession*)session
        self.subscriber = [[OTSubscriber alloc] initWithStream:stream
        [session subscribe:self.subscriber error:nil];
  6. Detect when the video from the subscriber gets decoded and add a view to your app to display it.
    - (void)subscriberVideoDataReceived:(OTSubscriber *)subscriber
        [subscriber.view setFrame:CGRectMake(10, 20, 175, 175)];
        [self.view addSubview:subscriber.view];
  7. Define the rest of the required methods, but leave them blank for now.
    - (void)sessionDidDisconnect:(OTSession*)session
        NSLog(@"Session disconnected");
    - (void)session:(OTSession*)session
        NSLog(@"session streamDestroyed (%@)", stream.streamId);
    - (void) session:(OTSession*)session
        NSLog(@"didFailWithError: (%@)", error);
    - (void)publisher:(OTPublisherKit*)publisher
      didFailWithError:(OTError*) error
        NSLog(@"publisher didFailWithError %@", error);
    - (void)subscriberDidConnectToStream:(OTSubscriberKit*)subscriber
        NSLog(@"subscriberDidConnectToStream (%@)",
    - (void)subscriber:(OTSubscriberKit*)subscriber
        NSLog(@"subscriber %@ didFailWithError %@",, error);

Building and running your app

That's it! Now just click the button to build and run your app in the Xcode Simulator. Because the Simulator does not have access to your camera, it will just show a spinning teapot.

How do I add a subscriber?

To get someone else to join the session, try using a supported iOS device. Alternatively, you can create a new project and follow the steps above. Just make sure to use the same API key and session ID. You can also use the Android or JavaScript hello world sample app to create a different client.

What's happening in this code?

What next?

Keep building

You can keep building on the Objective-C code above. Visit OpenTok Basics, Developer Guides, and Tutorials to find out how to add more functionality.

Get session IDs and tokens dynamically

Deploy one of our server SDKs to generate unique session IDs and per-user tokens on demand (required for a production app). Download a sample app in the language of your choice for a big head start.

Didn't find your favorite language? You can also access our REST API directly.