文章目录
方法
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 字段呢? 首先我想到的是,把系统语言转化为英文。可以看到如下效果:


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 中刚选中的 tabcom.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
Main | x-apple.systempreferences:com.apple.preference.universalaccess |
---|---|
Display | Seeing_Display |
Zoom | Seeing_Zoom |
VoiceOver | Seeing_VoiceOver |
Descriptions | Media_Descriptions |
Captions | Captioning |
Audio | Hearing |
Keyboard | Keyboard |
Mouse & Trackpad | Mouse |
Switch Control | Switch |
Dictation | SpeakableItems |
Security & Privacy Preference Pane
Main | x-apple.systempreferences:com.apple.preference.security |
---|---|
General | General |
FileVault | FDE |
Firewall | Firewall |
Advanced | Advanced |
Privacy | Privacy |
Privacy-Camera | Privacy_Camera |
Privacy-Microphone | Privacy_Microphone |
Automation | Privacy_Automation |
AllFiles | Privacy_AllFiles |
Accessibility | Privacy_Accessibility |
Assistive | Privacy_Assistive |
Location Services | Privacy_LocationServices |
SystemServices | Privacy_SystemServices |
Advertising | Privacy_Advertising |
Contacts | Privacy_Contacts |
Diagnostics & Usage | Privacy_Diagnostics |
Calendars | Privacy_Calendars |
Reminders | Privacy_Reminders |
Privacy_Facebook | |
Privacy_LinkedIn | |
Privacy_Twitter | |
Privacy_Weibo | |
Tencent Weibo | Privacy_TencentWeibo |
macOS Catalina 10.15:
ScreenCapture/Screen Recording | Privacy_ScreenCapture |
DevTools | Privacy_DevTools |
InputMonitoring | Privacy_ListenEvent |
DesktopFolder | Privacy_DesktopFolder |
DocumentsFolder | Privacy_DocumentsFolder |
DownloadsFolder | Privacy_DownloadsFolder |
NetworkVolume | Privacy_NetworkVolume |
RemovableVolume | Privacy_RemovableVolume |
SpeechRecognition | Privacy_SpeechRecognition |
Dictation & Speech Preference Pane
Main | x-apple.systempreferences:com.apple.preference.speech |
---|---|
Dictation | Dictation |
Text to Speech | TTS |
Sharing Preference Pane
Main | x-apple.systempreferences:com.apple.preferences.sharing |
---|---|
Screen Sharing | Services_ScreenSharing |
File Sharing | Services_PersonalFileSharing |
Printer Sharing | Services_PrinterSharing |
Remote Login | Services_RemoteLogin |
Remote Management | Services_ARDService |
Remote Apple Events | Services_RemoteAppleEvent |
Internet Sharing | Internet |
Bluetooth Sharing | Services_BluetoothSharing |