Screen Capture

In this guide we’ll show you how to share your iOS device application screen with other Participants connected to a Room. There are a couple of ways to do so. The simplest would be to use the TVIScreenCapturer API that ships with the Video iOS SDK. If you prefer, you can also implement a custom screen capturer.

In the example source code, you can flip a flag to choose to run the example project using either the TVIScreenCapturer API or the custom screen capturer (ExampleScreenCapturer).

Choose a Screen Capturer
var localVideoTrack: TVILocalVideoTrack?
var remoteView: TVIVideoView?
var screenCapturer: TVIVideoCapturer?
var webView: WKWebView?
var webNavigation: WKNavigation?

// Set this value to 'true' to use ExampleScreenCapturer instead of TVIScreenCapturer.
let useExampleCapturer = false
Using the Screen Capturer API

The TVIScreenCapturer class captures the contents of a single view at 1x scale. As this class depends on the UIKit and CoreGraphics frameworks to snapshot content, it does not function if the application is backgrounded. This class will use the dimensions of the view to determine its capture size and will not respond to TVIVideoConstraints.

To begin screen capture, initialize a TVILocalVideoTrack object with TVIVideoCapturer as an argument. Next, you need to setup a video renderer and add it to the TVILocalVideoTrack object created earlier.

Note: On iOS, capturing the screen requires no permissions from the user. While designing the UX, developers should be clear about their intentions to the user.

Begin Screen Capture
func setupLocalMedia() {
    // Setup screen capturer
    let capturer: TVIVideoCapturer
    if (useExampleCapturer) {
        capturer = ExampleScreenCapturer.init(aView: self.webView!)
    } else {
        capturer = TVIScreenCapturer.init(view: self.webView!)
    }

    localVideoTrack = TVILocalVideoTrack.init(capturer: capturer, enabled: true, constraints: nil, name: "Screen")

    if (localVideoTrack == nil) {
        presentError(message: "Failed to add screen capturer track!")
        return;
    }

    screenCapturer = capturer;

    // Setup rendering
    remoteView = TVIVideoView.init(frame: CGRect.zero, delegate: self)
    localVideoTrack?.addRenderer(remoteView!)

    remoteView!.isHidden = true
    self.view.addSubview(self.remoteView!)
    self.view.setNeedsLayout()
}

Found an error ? Open an issue on Github

Stuck on something? Can't find what you're looking for? Don't hesitate to reach out to us at help@twilio.com and we'll happily give you a hand.

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.