Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now

Menu

Expand
Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Screen Capture

Deprecation Notice - all 1.x Versions
Please note that all Programmable Video iOS SDK 1.x versions are deprecated. Version 3.x is the latest version of the Programmable Video iOS SDK. Please see the Getting Started Guide for more information.

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.

Rate this page:

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.