How to build a basic app with GoCoder SDK for iOS

Learn how to use Xcode to build a basic streaming app that uses Wowza GoCoder™ SDK for iOS.

Contents


Create an app project
Add a broadcast button
Define app properties
Register and initialize the SDK
Check for app permissions
Start the camera preview
Configure a broadcast stream
Add broadcast monitoring callbacks
Start streaming
Build and deploy an app
Example ViewController class

Create an app project


First, you must create an app project in Xcode.

  1. In Xcode, click File, click New, and then select Project to create a new project.
  2. Select Single View Application and then click Next.
  3. In the project options dialog box, enter a Product Name and Organization Name. The Bundle Identifier must match the identifier used to generate your SDK license key.
  4. Click Next, specify where you want your source repository to be located, and then click Create.
  5. Make sure the GoCoder SDK library is installed in the project folder according to one of the options in How to install GoCoder SDK for iOS.

Add a broadcast button


Next, add a broadcast button to the app's interface.

  1. In the Project navigator, select Main.storyboard and then the View Controller.
  2. Drag a button from the Object library onto the Interface Builder canvas.
  3. Select the Button text and rename it Broadcast.
  4. Add a referencing outlet called broadcastButton to ViewController.m.
    @interface ViewController ()
    
    // Referencing outlet for the broadcast button
    @property (weak, nonatomic) IBOutlet UIButton *broadcastButton;
    
    @end

Define app properties


If you didn't do so when installing GoCoder SDK, add an #import statement to the GoCoder SDK's API header, and then add the goCoder property to the ViewController class defined in ViewController.m.

#import <WowzaGoCoderSDK/WowzaGoCoderSDK.h>

@interface ViewController ()

// The top level GoCoder API interface
@property (nonatomic, strong) WowzaGoCoder *goCoder;

// Referencing outlet for the broadcast button
@property (weak, nonatomic) IBOutlet UIButton *broadcastButton;

@end

Register and initialize the SDK


Then, you must register your GoCoder SDK license and initialize the SDK by adding the following to the viewDidLoad method of the ViewController class in ViewController.m, replacing GOSK-XXXX-XXXX-XXXX-XXXX-XXXX with your registered license key.

// Register the GoCoder SDK license key
NSError *goCoderLicensingError = [WowzaGoCoder registerLicenseKey:@"GOSK-XXXX-XXXX-XXXX-XXXX-XXXX"];
if (goCoderLicensingError != nil) {
    // Log license key registration failure
    NSLog(@"%@", [goCoderLicensingError localizedDescription]);
} else {
    // Initialize the GoCoder SDK
    self.goCoder = [WowzaGoCoder sharedInstance];
}

Check for app permissions


For iOS 10 and later, you must define the messages that are displayed to users when the app requests permissions to access the camera and microphone.

  1. Select your build target in the Project Inspector and click the Info tab.
  2. Add the following Custom iOS Target Properties:
    • Add a string-valued key named Privacy - Camera Usage Description with the value The camera will be used to capture video for live streaming.
    • Add a string-valued key named Privacy - Microphone Usage Description with the value The microphone will be used to capture audio for live streaming.

Start the camera preview


Next, associate the UI view with the SDK's camera preview by adding the following to the viewDidLoad method of the ViewController class in ViewController.m.

if (self.goCoder != nil) {
   // Associate the U/I view with the SDK camera preview
   self.goCoder.cameraView = self.view;

   // Start the camera preview
   [self.goCoder.cameraPreview startPreview];
}

Configure a broadcast stream


Add the hostAddress, portNumber, applicationName, and streamName for your Wowza Streaming Engine™ server or Wowza Streaming Cloud™ account to the viewDidLoad method of the ViewController class in ViewController.m.

Note: If you're using Wowza Streaming Cloud, the Connection Code is on the Overview tab of the live stream detail page in the Wowza Streaming Cloud web interface.

// Get a copy of the active config
WowzaConfig *goCoderBroadcastConfig = self.goCoder.config;

// Set the defaults for 720p video
[goCoderBroadcastConfig loadPreset:WZFrameSizePreset1280x720];

// Set the connection properties for the target Wowza Streaming Engine server or Wowza Cloud account
goCoderBroadcastConfig.hostAddress = @"live.someserver.net";
goCoderBroadcastConfig.portNumber = 1935;
goCoderBroadcastConfig.applicationName = @"live";
goCoderBroadcastConfig.streamName = @"myStream";

// Update the active config
self.goCoder.config = goCoderBroadcastConfig;

Add broadcast monitoring callbacks


Next, add callbacks to monitor the broadcast's status updates and errors.

  1. Update the interface definition for the ViewController class to include the WZStatusCallback protocol, which monitors status updates and errors during live stream broadcast.
    // Implements the WZStatusCallback protocol
    @interface ViewController () <WZStatusCallback>
  2. Add the methods defined by the WZStatusCallback protocol to the ViewController class to monitor status updates and errors during a live streaming broadcast.
    - (void) onWZStatus:(WZStatus *) goCoderStatus {
        // A successful status transition has been reported by the GoCoder SDK
        NSString *statusMessage = nil;
    
        switch (goCoderStatus.state) {
            case WZStateIdle:
                statusMessage = @"The broadcast is stopped";
                break;
    
            case WZStateStarting:
                statusMessage = @"Broadcast initialization";
                break;
    
            case WZStateRunning:
                statusMessage = @"Streaming is active";
                break;
    
            case WZStateStopping:
                statusMessage = @"Broadcast shutting down";
                break;
        }
    
        if (statusMessage != nil)
            NSLog(@"Broadcast status: %@", statusMessage);
    }
    
    - (void) onWZError:(WZStatus *) goCoderStatus {
        // If an error is reported by the GoCoder SDK, display an alert dialog
        // containing the error details using the U/I thread
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView *alertDialog =
                [[UIAlertView alloc] initWithTitle:@"Streaming Error"
                                      message:goCoderStatus.description
                                     delegate:nil
                            cancelButtonTitle:@"OK"
                            otherButtonTitles:nil];
            [alertDialog show];
        });
    }

Start streaming


Before streaming, you must configure the broadcast button to start and stop the stream when pressed.

  1. Add an event handler method to the ViewController class to be invoked when the broadcast button is pressed.
    - (IBAction)broadcastButtonTapped:(UIButton *)button
    {
        // Ensure the minimum set of configuration settings have been specified necessary to
        // initiate a broadcast streaming session
        NSError *configValidationError = [self.goCoder.config validateForBroadcast];
    
        if (configValidationError != nil) {
            UIAlertView *alertDialog =
                [[UIAlertView alloc] initWithTitle:@"Incomplete Streaming Settings"
                            message: self.goCoder.status.description
                            delegate:nil
                            cancelButtonTitle:@"OK"
                            otherButtonTitles:nil];
            [alertDialog show];
        } else if (self.goCoder.status.state != WZStateRunning) {
            // Start streaming
            [self.goCoder startStreaming:self];
        }
        else {
            // Stop the broadcast that is currently running
            [self.goCoder endStreaming:self];
        }    
    }
  2. Associate the broadcastButtonTapped event handler with the broadcast button by adding the following to the bottom of the viewDidLoad method.
    [self.broadcastButton addTarget:self action:@selector(broadcastButtonTapped:)
        forControlEvents:UIControlEventTouchUpInside];

Build and deploy an app


Finally, you're ready to build and deploy the app. Click the Product menu and select Run.

Example ViewController class


The following is an example of the ViewController class with all of the additions described in this article.

#import "ViewController.h"
#import <WowzaGoCoderSDK/WowzaGoCoderSDK.h>

@interface ViewController () <WZStatusCallback>

// The top level GoCoder API interface
@property (nonatomic, strong) WowzaGoCoder *goCoder;

// Referencing outlet for the broadcast button
@property (weak, nonatomic) IBOutlet UIButton *broadcastButton;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.

  // Register the GoCoder SDK license key
  NSError *goCoderLicensingError = [WowzaGoCoder registerLicenseKey:@"GOSK-XXXX-XXXX-XXXX-XXXX-XXXX"];
  if (goCoderLicensingError != nil) {
    // Log license key registration failure
    NSLog(@"%@", [goCoderLicensingError localizedDescription]);
  } else {
    // Initialize the GoCoder SDK
    self.goCoder = [WowzaGoCoder sharedInstance];
  }

  if (self.goCoder != nil) {
    // Associate the U/I view with the SDK camera preview
    self.goCoder.cameraView = self.view;

    // Start the camera preview
    [self.goCoder.cameraPreview startPreview];
  }

  // Get a copy of the active config
  WowzaConfig *goCoderBroadcastConfig = self.goCoder.config;

  // Set the defaults for 720p video
  [goCoderBroadcastConfig loadPreset:WZFrameSizePreset1280x720];

  // Set the connection properties for the target Wowza Streaming Engine server or Wowza Cloud account
  goCoderBroadcastConfig.hostAddress = @"live.someserver.net";
  goCoderBroadcastConfig.portNumber = 1935;
  goCoderBroadcastConfig.applicationName = @"live";
  goCoderBroadcastConfig.streamName = @"myStream";

  // Update the active config
  self.goCoder.config = goCoderBroadcastConfig;

  [self.broadcastButton addTarget:self action:@selector(broadcastButtonTapped:)
           forControlEvents:UIControlEventTouchUpInside];
}


- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

- (void) onWZStatus:(WZStatus *) goCoderStatus {
  // A successful status transition has been reported by the GoCoder SDK
  NSString *statusMessage = nil;

  switch (goCoderStatus.state) {
    case WZStateIdle:
      statusMessage = @"The broadcast is stopped";
      break;

    case WZStateStarting:
      statusMessage = @"Broadcast initialization";
      break;

    case WZStateRunning:
      statusMessage = @"Streaming is active";
      break;

    case WZStateStopping:
      statusMessage = @"Broadcast shutting down";
      break;
  }

  if (statusMessage != nil)
    NSLog(@"Broadcast status: %@", statusMessage);
}

- (void) onWZError:(WZStatus *) goCoderStatus {
  // If an error is reported by the GoCoder SDK, display an alert dialog
  // containing the error details using the U/I thread
  dispatch_async(dispatch_get_main_queue(), ^{
    UIAlertView *alertDialog =
    [[UIAlertView alloc] initWithTitle:@"Streaming Error"
                   message:goCoderStatus.description
                  delegate:nil
             cancelButtonTitle:@"OK"
             otherButtonTitles:nil];
    [alertDialog show];
  });
}

- (IBAction)broadcastButtonTapped:(UIButton *)button
{
  // Ensure the minimum set of configuration settings have been specified necessary to
  // initiate a broadcast streaming session
  NSError *configValidationError = [self.goCoder.config validateForBroadcast];

  if (configValidationError != nil) {
    UIAlertView *alertDialog =
    [[UIAlertView alloc] initWithTitle:@"Incomplete Streaming Settings"
                   message: self.goCoder.status.description
                  delegate:nil
             cancelButtonTitle:@"OK"
             otherButtonTitles:nil];
    [alertDialog show];
  } else if (self.goCoder.status.state != WZStateRunning) {
    // Start streaming
    [self.goCoder startStreaming:self];
  }
  else {
    // Stop the broadcast that is currently running
    [self.goCoder endStreaming:self];
  }    
}

@end

If you're having problems or want to discuss this article, post in our forum.