Capture screenshot in MacOS App issue

Hi,

I am trying to capture screenshot in MacOS App with sand box.

I am getting background screen only.

can any one tell me best way to capture complete screenshot.

Answered by DTS Engineer in 745006022

Weird.

Try extracting your code into a new test app, starting from the macOS > App template in Xcode. Make sure to set up Apple Development signing before you run it the first time. Does that reproduce the problem?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

What have you tried, CGDisplayCreateImage? You may also need CGPreflightScreenCaptureAccess and CGRequestScreenCaptureAccess.

Hi,

Following is my current code which is trying to implement in macOS App

      var displayCount: UInt32 = 0;
      var result = CGGetActiveDisplayList(0, nil, &displayCount)
      if result != CGError.success {
        print("error: \(result)")
        return
      }
      let allocated = Int(displayCount)
      let activeDisplays = UnsafeMutablePointer<CGDirectDisplayID>.allocate(capacity: allocated)
      result = CGGetActiveDisplayList(displayCount, activeDisplays, &displayCount)
       
      if result != CGError.success {
        print("error: \(result)")
        return
      }
       
            
      for i in 1...displayCount {
               
         
        let fileUrl = URL(fileURLWithPath: "image\(i).jpg", isDirectory: true)
         
        let screenShot:CGImage = CGDisplayCreateImage(activeDisplays[Int(i-1)])!
        let bitmapRep = NSBitmapImageRep(cgImage: screenShot)
        let jpegData = bitmapRep.representation(using: NSBitmapImageRep.FileType.jpeg, properties: [:])!
                   
        do {
          try jpegData.write(to: fileUrl, options: .atomic)
        } catch { print("error: \(error)") }
      }

Note: I am implement same code in console app separately which it woks fine, but issue in macOS App

Try calling CGRequestScreenCaptureAccess before the other stuff. That’s probably needed to do screen capture from within the sandbox.

Tried using CGRequestScreenCaptureAccess first time it returns true, then after there is no return from that function which leads to stop executing the code.

As per my functionality calling permissions every time when application starts and executing particular code means it will be issue to user.

Is there any option to authenticate for the first time then after it need to run without authentication.

Is there any option to authenticate for the first time then after it need to run without authentication.

That’s the default behaviour.

If you’re seeing the system ‘forget’ this privilege each time you run your app, that’s most likely because it’s not signed with a stable code signing identity. I recommend Apple Development signing for day-to-day work.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

ok i will try with signed app

Tried signed macOS App still same issue.

Accepted Answer

Weird.

Try extracting your code into a new test app, starting from the macOS > App template in Xcode. Make sure to set up Apple Development signing before you run it the first time. Does that reproduce the problem?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Capture screenshot in MacOS App issue
 
 
Q