Record only capture the main window using SwiftUI

277 views Asked by At

The ReplayKit recording will only capture the main window of your application. When designing for screen recording, you can place any UI elements in a separate UIWindow object. Although applications typically only have one UIWindow, iOS allows many windows. A UIWindow requires a .rootViewController to provide content.

How do I create another UIWindow in SwiftUI?

According to this article it is possible to implement it on UIKit.

main view controls UIWindow and player UIWindow

I tried adding a new UIWindow like this and it didn't work.

extension UIApplication {
    
    var windowScene: UIWindowScene? {
        // Get connected scenes
        return UIApplication.shared.connectedScenes
            // Keep only active scenes, onscreen and visible to the user
            .filter { $0.activationState == .foregroundActive }
            // Keep only the first `UIWindowScene`
            .first(where: { $0 is UIWindowScene })
            // Get its associated windows
            .flatMap({ $0 as? UIWindowScene })
    }
    
    
}

struct ContentView: View {
    var body: some View {
        VStack {
            Button("Open New Window") {
                if let windowScene = UIApplication.shared.windowScene {
                    let newWindow = UIWindow(frame: UIScreen.main.bounds)
                    newWindow.windowScene = windowScene
                    newWindow.rootViewController = UIHostingController(rootView:  Text("This is another view!"))
                    newWindow.makeKeyAndVisible()
                }
            }
        }
    }
}
1

There are 1 answers

2
Zeeshan Ahmad II On

In SwiftUI, the recommended approach for managing multiple windows is by using the @State property wrapper and WindowGroup structure provided by the SwiftUI framework. Here's an example of how you can create and manage multiple windows in SwiftUI:

import SwiftUI

@main
struct YourApp: App {
    @State private var playerWindow: UIWindow?
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onAppear {
                    createPlayerWindow()
                }
        }
        .onChange(of: playerWindow) { newPlayerWindow in
            if let window = newPlayerWindow {
                // Handle the appearance or dismissal of the player window
                window.isHidden = false
            } else {
                // Player window dismissed
                // Clean up resources or perform any necessary actions
            }
        }
    }
    
    private func createPlayerWindow() {
        let window = UIWindow(frame: UIScreen.main.bounds)
        window.rootViewController = UIHostingController(rootView: PlayerView())
        
        // Configure any additional window properties if needed
        
        playerWindow = window
    }
}

struct ContentView: View {
    var body: some View {
        Text("Main Content View")
    }
}

struct PlayerView: View {
    var body: some View {
        Text("Player View")
    }
}