macOS 开发 - 10.15 Screen Recording 屏幕录制 授权及跳转


方法

1、权限判断

在 SIP 下(正常情况下):


- (BOOL)canRecord{
    CGDisplayStreamRef stream = CGDisplayStreamCreate(CGMainDisplayID() , 1, 1, kCVPixelFormatType_32ABGR, nil, ^(CGDisplayStreamFrameStatus status, uint64_t displayTime, IOSurfaceRef  _Nullable frameSurface, CGDisplayStreamUpdateRef  _Nullable updateRef) {
        
    });
    
    BOOL canRecord = stream != NULL;
    if (stream) {
        CFRelease(stream);
    }
    
    NSLog(@"canRecord : %ld",canRecord);
    return canRecord;
}

如果不灵敏,试试用 tccutil reset All com.xx.xx 清理授权试试。


在关闭 SIP 的情况下,可以通过读取 tcc.data 得到所有授权。
如果返回1,代表授权成功;返回 0 或者什么也没返回,代表没有授权。具体有没有用户操作,需要进一步辨别。

$ sqlite3 /Library/Application\ Support/com.apple.TCC/TCC.db "SELECT  allowed  from access WHERE client='com.ms.AuthDemo' AND service='kTCCServiceScreenCapture';"

2、申请授权

调用截屏方法,会调起系统的申请权限窗口;
但这里无论是否授权成功,都能够截屏。
所以这个方法不适宜做权限判断。

- (void)showScreenRecordingPrompt{
  
  /* macos 10.14 and lower do not require screen recording permission to get window titles */
  if(@available(macos 10.15, *)) {
    /*
     To minimize the intrusion just make a 1px image of the upper left corner
     This way there is no real possibilty to access any private data
     */
    CGImageRef c = CGWindowListCreateImage(
                                                    CGRectMake(0, 0, 1, 1),
                                                    kCGWindowListOptionOnScreenOnly,
                                                    kCGNullWindowID,
                                                    kCGWindowImageDefault);
  
      CFRelease(screenshot);

}


3、隐私面板跳转

- (void)openSetting{
    NSString *urlString = @"x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture";
    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:urlString]];
}

探索经历

1、关于 10.15 权限

建议说,目前不要将主力机升级到 10.15。可是升级后,才发现非主力机也是那么重要。遇到各种问题的时候,由衷的觉得自己真是个 SB。
不过对于兼容今秋的系统,这也是必不可少的。10.14 开始对权限越来越严格,10.15 中又新增了部分权限。

如果只是打开面板,是非常容易的。但需要精准打开对应的tab才更方便用户使用。如何获取这些Privacy 字段呢? 首先我想到的是,把系统语言转化为英文。可以看到如下效果:

Sample Sample

2、关于隐私面板跳转

Microphone 这种跳转是非常直接的,地址接 Privacy_Microphone 即可:
@"x-apple.systempreferences:com.apple.preference.security?Privacy_Microphone";

那么 Screen Recording 呢?自然有以下猜想

  • Privacy_ScreenRecording
  • Privacy_Screen_Recording
  • Privacy_Screen\n Recording

当然,都是无效的;

那么如何获取这个权限对应的名字呢?


3、获取偏好设置plist信息

存在一个这样的 plist 文件,是否保存了我们的配置信息呢?
~/Library/Preferences/com.apple.systempreferences.plist

显然没有。

通过勾选/取消部分权限,数据并没保存在这里。这里只是保存了这个面板的使用数据。如:

  • SecurityPrefTab 代表在 security 中刚选中的 tab
  • com.apple.SecurityPref.Privacy.LastSourceSelected 代表在 Privacy 中选中的第几个item。

4、tcc

之前在 《macOS 开发 - NSMicrophoneUsageDescription (10.14 mojave 权限问题》https://blog.csdn.net/lovechris00/article/details/83542730 中写过,如果像清理授权记录,需要用到 tccutil 命令。(现在依然没搞清楚 tcc 是什么的缩写,知道的小伙伴给我留言吧)。那么tcc 中是否有相关信息呢?
man tccutil 中并没有给我类似的信息,感觉 tccutil 只是用来清理,而无法添加。


但 tcc 数据库确是用来管理授权数据的,下面两个db管理了授权信息:
~/Library/Application Support/com.apple.TCC/TCC.db
/Library/Application Support/com.apple.TCC/TCC.db

在这里插入图片描述


一个可以增删改查的 tccutil
https://github.com/jacobsalmela/tccutil


5、使用 AppleScript 获取偏好设置面板信息

https://apple.stackexchange.com/questions/361045/open-system-preferences-privacy-full-disk-access-with-url
stackOverFlow 中上述方法为使用AppleScript :

tell application "System Preferences" to get name of anchors of current pane

尝试了,果然看到了一些熟悉的字眼:
但缺点是,数据是不全的。

在这里插入图片描述


6、OS X System Preference Links

关于 Mac 上的“隐私”偏好设置
https://support.apple.com/zh-cn/guide/mac-help/mh32356/mac


遇到一个这样的网站,给出了一些privacy字段:
https://macosxautomation.com/system-prefs-links.html

https://stackoverflow.com/questions/6652598/cocoa-button-opens-a-system-preference-page


Accessibility Preference Pane

Mainx-apple.systempreferences:com.apple.preference.universalaccess
DisplaySeeing_Display
ZoomSeeing_Zoom
VoiceOverSeeing_VoiceOver
DescriptionsMedia_Descriptions
CaptionsCaptioning
AudioHearing
KeyboardKeyboard
Mouse & TrackpadMouse
Switch ControlSwitch
DictationSpeakableItems

Security & Privacy Preference Pane

Mainx-apple.systempreferences:com.apple.preference.security
GeneralGeneral
FileVaultFDE
FirewallFirewall
AdvancedAdvanced
PrivacyPrivacy
Privacy-CameraPrivacy_Camera
Privacy-MicrophonePrivacy_Microphone
AutomationPrivacy_Automation
AllFilesPrivacy_AllFiles
AccessibilityPrivacy_Accessibility
AssistivePrivacy_Assistive
Location ServicesPrivacy_LocationServices
SystemServicesPrivacy_SystemServices
AdvertisingPrivacy_Advertising
ContactsPrivacy_Contacts
Diagnostics & UsagePrivacy_Diagnostics
CalendarsPrivacy_Calendars
RemindersPrivacy_Reminders
FacebookPrivacy_Facebook
LinkedInPrivacy_LinkedIn
TwitterPrivacy_Twitter
WeiboPrivacy_Weibo
Tencent WeiboPrivacy_TencentWeibo

macOS Catalina 10.15:

ScreenCapture/Screen RecordingPrivacy_ScreenCapture
DevToolsPrivacy_DevTools
InputMonitoringPrivacy_ListenEvent
DesktopFolderPrivacy_DesktopFolder
DocumentsFolderPrivacy_DocumentsFolder
DownloadsFolderPrivacy_DownloadsFolder
NetworkVolumePrivacy_NetworkVolume
RemovableVolumePrivacy_RemovableVolume
SpeechRecognitionPrivacy_SpeechRecognition

Dictation & Speech Preference Pane

Mainx-apple.systempreferences:com.apple.preference.speech
DictationDictation
Text to SpeechTTS

Sharing Preference Pane

Mainx-apple.systempreferences:com.apple.preferences.sharing
Screen SharingServices_ScreenSharing
File SharingServices_PersonalFileSharing
Printer SharingServices_PrinterSharing
Remote LoginServices_RemoteLogin
Remote ManagementServices_ARDService
Remote Apple EventsServices_RemoteAppleEvent
Internet SharingInternet
Bluetooth SharingServices_BluetoothSharing
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI工程仔

请我喝杯伯爵奶茶~!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值