mac程序经常会用到要求用户更改安全设置、隐私设置等问题的情况,所以需要弹出系统设置界面让不精通mac操作的用户能够轻而易举的修改偏好设置。

使用URL Scheme

首先我们想到的就是使用urlScheme来打开窗口。在swiftUI下使用urlScheme非常容易。

我们需要在结构体中引用

1
@Environment(\.openURL) var openURL

之后我们来打开url

1
openURL(URL(string: "https://blog.zhheo.com/")!)

我们就完成了一个打开网页的操作。将网页url更换成urlScheme同样可用。

那么需要更改成什么url呢?

URL Scheme 格式

部分控制面板支持通过 URL Scheme 进行访问。在各个控制面板对应的 .prefpane 文件里的 Info.plist 中,如果有下面这一键值对,则表示这个面板支持 URL Scheme:

1
2
<key>NSPrefPaneAllowsXAppleSystemPreferencesURLScheme</key>
<true/>

控制面板的 URL Scheme 格式是 x-apple.systempreferences: 前缀加上面板的 Bundle Identifier:

格式

有些面板支持锚点,可以在 URL Scheme 后面加入锚点名称来访问控制面板的某个区域:

格式

Siri Pane

1
com.apple.preference.speech

Notifications Pane

1
com.apple.preference.notifications

Accessibility Pane

1
com.apple.preference.universalaccess
  • Display com.apple.preference.universalaccess?Seeing_Display
  • Zoom com.apple.preference.universalaccess?Seeing_Zoom
  • VoiceOver com.apple.preference.universalaccess?Seeing_VoiceOver
  • Descriptions com.apple.preference.universalaccess?Media_Descriptions
  • Captions com.apple.preference.universalaccess?Captioning
  • Audio com.apple.preference.universalaccess?Hearing
  • Keyboard com.apple.preference.universalaccess?Keyboard
  • Mouse & Trackpad com.apple.preference.universalaccess?Mouse
  • Switch Control com.apple.preference.universalaccess?Switch
  • Dictation com.apple.preference.universalaccess?SpeakableItems

Screen Time

1
com.apple.preference.screentime

Main Tabs

1
com.apple.preference.security
  • General com.apple.preference.security?General
  • Advanced com.apple.preference.security?Advanced
  • FileVault com.apple.preference.security?FDE
  • Firewall com.apple.preference.security?Firewall
  • Privacy Main com.apple.preference.security?Privacy

Privacy Anchors

  • Location Services com.apple.preference.security?Privacy_LocationServices
  • Contacts com.apple.preference.security?Privacy_Contacts
  • Calendars com.apple.preference.security?Privacy_Calendars
  • Reminders com.apple.preference.security?Privacy_Reminders
  • Photos com.apple.preference.security?Privacy_Photos
  • Camera com.apple.preference.security?Privacy_Camera
  • Microphone com.apple.preference.security?Privacy_Microphone
  • Speech Recognition com.apple.preference.security?Privacy_SpeechRecognition
  • Accessibility com.apple.preference.security?Privacy_Accessibility
  • Input Monitoring Not Available
  • Full Disk Access com.apple.preference.security?Privacy_AllFiles
  • Files and Folders Not Available
  • Screen Recording Not Available
  • Automation com.apple.preference.security?Privacy_Automation
  • Analytics and Improvements com.apple.preference.security?Privacy_Diagnostics
  • Advertising com.apple.preference.security?Privacy_Advertising

Keyboard Pane

  • Dictation com.apple.preference.speech?Dictation

Sharing Preference Pane

1
com.apple.preferences.sharing
  • Screen Sharing com.apple.preferences.sharing?Services_ScreenSharing
  • File Sharing com.apple.preferences.sharing?Services_PersonalFileSharing
  • Printer Sharing com.apple.preferences.sharing?Services_PrinterSharing
  • Remote Login com.apple.preferences.sharing?Services_RemoteLogin
  • Remote Management com.apple.preferences.sharing?Services_ARDService
  • Remote Apple Events com.apple.preferences.sharing?Services_RemoteAppleEvent
  • Internet Sharing com.apple.preferences.sharing?Internet
  • Bluetooth Sharing com.apple.preferences.sharing?Services_BluetoothSharing

参考文档

使用代码打开指定偏好设置面板

macOS 10.15 System Preference Panes