How to capture the screen on mac using SwiftUI?

I am a beginner in SwiftUI. My task to capture the image of the whole screen using SwiftUI includes other apps and title bar. I am unable to show the permission dialog of screen recording and capturing the image of the screen. I have implemented the solution below but did not get success.

func screenshotWindowAndSuccess() -> Bool {

    let task = Process()
    task.launchPath  = "/usr/sbin/screencapture"
    task.arguments = ["-cm"]
    task.launch()
    task.waitUntilExit()
    let status = task.terminationStatus
    return status == 0
}

Please help me to get out of this task. Thank you in advance!

Have a look here. https://stackoverflow.com/questions/39691106/programmatically-screenshot-swift-3-macos

It is Swift, but you could call the func in a button from SwiftUI.

How to capture the screen on mac using SwiftUI?
 
 
Q