macOS 開發 - 10.15 Screen Recording 授權及跳轉


方法

1、權限判斷

終端輸入下述命令,可以得到 屏幕錄製 的權限結果。
如果返回1,代表授權成功;返回 0 或者什麼也沒返回,代表沒有授權。具體有沒有用戶操作,需要進一步辨別。

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

終端命令在代碼中用 NSTask 執行即可。


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

遇到一個這樣的網站,給出了一些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
Facebook Privacy_Facebook
LinkedIn Privacy_LinkedIn
Twitter Privacy_Twitter
Weibo 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章