title: Detecting screen recording settings on macOS Catalina
body: <p>What's is a reliable way to detect if user has enabled this API?</p>
  
  <p><code>CGWindowListCreateImage</code> returns a valid object even if screen recording API is disabled. There are multiple combinations possible (<code>kCGWindowListOptionIncludingWindow</code>, <code>kCGWindowListOptionOnScreenBelowWindow</code>) and only some will return NULL. </p>
  
  <pre><code>- (CGImageRef)createScreenshotImage
  {
      NSWindow *window = [[self view] window];
      NSRect rect = [window frame];
  
      rect.origin.y = NSHeight([[window screen] frame]) - NSMaxY([window frame]);
      CGImageRef screenshot = CGWindowListCreateImage(
                                                      rect,
                                                      kCGWindowListOptionIncludingWindow,
                                                      //kCGWindowListOptionOnScreenBelowWindow,
                                                      0,//(CGWindowID)[window windowNumber],
                                                      kCGWindowImageBoundsIgnoreFraming);//kCGWindowImageDefault
      return screenshot;
  }
  </code></pre>
  
  <p>The only reliable way is through <code>CGDisplayStreamCreate</code> which is risky as Apple always changes privacy settings every year.</p>
  
  <pre><code>   - (BOOL)canRecordScreen
      {
          if (@available(macOS 10.15, *)) {
              CGDisplayStreamRef stream = CGDisplayStreamCreate(CGMainDisplayID(), 1, 1, kCVPixelFormatType_32BGRA, nil, ^(CGDisplayStreamFrameStatus status, uint64_t displayTime, IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef) {
                  ;
              });
              BOOL canRecord = stream != NULL;
              if (stream) { 
                CFRelease(stream); 
              }
              return canRecord;
          } else {
              return YES;
          }
      }
  </code></pre>