Expose the Root View for Full-Screen Capture
To capture the entire app screen, you need access to the root UIView. Add an extension:
UIApplication+rootViewController.swift
import UIKit
extension UIApplication {
var rootViewController: UIViewController? {
connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap { $0.windows }
.first { $0.isKeyWindow }?
.rootViewController
}
}
Then use:
guard let rootView = UIApplication.shared.rootViewController?.view else { return }
capturer = ScreenCapturer(withView: rootView)
Alternative path: Capture a specific region with UIViewRepresentable
In following steps we will show you to how to wire the rootView into the ScreenCapturer. However in case you prefer to capture a specific region of the screen - you can capture a specific UIView backed by SwiftUI. Create a UIViewRepresentable that produces the view you want to share, embed it in your layout, and pass its underlying UIView to the capturer (e.g. via a callback when makeUIView runs).
Example - a sharable time label:
import SwiftUI
import UIKit
struct TimeLabelView: UIViewRepresentable {
let text: String
private let labelTag = 1001
func makeUIView(context: Context) -> UIView {
let container = UIView()
container.backgroundColor = .red
let label = UILabel()
label.tag = labelTag
label.textAlignment = .center
label.backgroundColor = .clear
label.textColor = .white
label.adjustsFontSizeToFitWidth = true
label.translatesAutoresizingMaskIntoConstraints = false
container.addSubview(label)
NSLayoutConstraint.activate([
label.leadingAnchor.constraint(equalTo: container.leadingAnchor),
label.trailingAnchor.constraint(equalTo: container.trailingAnchor),
label.topAnchor.constraint(equalTo: container.topAnchor),
label.bottomAnchor.constraint(equalTo: container.bottomAnchor)
])
return container
}
func updateUIView(_ uiView: UIView, context: Context) {
(uiView.viewWithTag(labelTag) as? UILabel)?.text = text
}
}
Display TimeLabelView in SwiftUI and wire its UIView into your manager (e.g. via @State and an onAppear / Coordinator), then pass that view to ScreenCapturer(withView:) to share only that region instead of the full screen.
Screen sharing
Learn how to implement tha screensharing capability using the Vonage Video API platform.