首页 > 解决方案 > 在 Catalina (kCGWindowName) 中启用屏幕录制 API

问题描述

以下代码在 Mojave 中运行良好。

let options = CGWindowListOption(arrayLiteral: CGWindowListOption.excludeDesktopElements, CGWindowListOption.optionOnScreenOnly, CGWindowListOption.optionOnScreenAboveWindow)
let windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID) as NSArray? as? [[String: AnyObject]]

for entry  in windowList!
{ let name = (entry[kCGWindowName as String] != nil) ? entry[kCGWindowName as String] as! String : ""
  ...

在卡塔利娜

entry[kCGWindowName as String] 

总是零

在 SO:Detecting screen recording settings on macOS Catalina我读过,这不是启用“屏幕录制 API”以及如何检测(如果已启用)所必需的。

不幸的是,我不知道如何启用“屏幕录制 API”。

如 SO:macOS Catalina 屏幕录制权限中所述,我打开了自动代码签名。

在系统偏好设置中,我看不到“+”来添加应用程序,以授予“屏幕录制”。

如何授予录屏 API 权限?

标签: macoscocoamacos-catalinascreen-recording

解决方案


This a low level api to request access to screen recording. Calling this functions will present a prompt to give access for screen recording.

/* Requests event listening access if absent, potentially prompting */
@available(macOS 10.15, *)
public func CGRequestScreenCaptureAccess() -> Bool

推荐阅读