feat(权限): 添加权限检查和管理功能

添加权限检查功能,包括自动启动和系统命令执行权限
在菜单中添加打开系统设置和登录项管理的按钮
This commit is contained in:
2025-12-27 19:57:52 +08:00
parent 21bb3131a0
commit be65e275b3
2 changed files with 108 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ import Foundation
import Combine import Combine
import IOKit.pwr_mgt import IOKit.pwr_mgt
import ServiceManagement import ServiceManagement
import AppKit
// MARK: - App State Manager // MARK: - App State Manager
class AppState: ObservableObject { class AppState: ObservableObject {
@@ -191,6 +192,62 @@ class AppState: ObservableObject {
} }
} }
// MARK: - Permission Management
///
func checkPermissions() {
print("检查应用程序权限...")
//
checkLaunchAtLoginStatus()
print("自动启动权限: \(isLaunchAtLogin ? "已启用" : "已禁用")")
//
let canExecuteSystemCommands = checkSystemCommandPermission()
print("系统命令执行权限: \(canExecuteSystemCommands ? "已允许" : "已拒绝")")
}
///
private func checkSystemCommandPermission() -> Bool {
//
let process = Process()
process.executableURL = URL(fileURLWithPath: "/bin/echo")
process.arguments = ["test"]
do {
try process.run()
process.waitUntilExit()
return process.terminationStatus == 0
} catch {
print("执行系统命令失败: \(error)")
return false
}
}
///
func openSecuritySettings() {
print("打开系统设置的隐私与安全性页面...")
let settingsUrl = URL(string: "x-apple.systempreferences:com.apple.preference.security")!
if NSWorkspace.shared.open(settingsUrl) {
print("已打开系统设置页面")
} else {
print("打开系统设置页面失败")
}
}
///
func openLoginItemsSettings() {
print("打开系统设置的登录项页面...")
let settingsUrl = URL(string: "x-apple.systempreferences:com.apple.preference.usersandgroups?LoginItems")!
if NSWorkspace.shared.open(settingsUrl) {
print("已打开登录项设置页面")
} else {
print("打开登录项设置页面失败")
}
}
// MARK: - Deinitializer // MARK: - Deinitializer
deinit { deinit {

View File

@@ -121,6 +121,57 @@ struct MenuContent: View {
Divider() Divider()
//
VStack(alignment: .leading, spacing: 4) {
Text("权限管理")
.font(.caption)
.foregroundColor(.secondary)
//
Button(action: {
//
let settingsUrl = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Automation")!
if NSWorkspace.shared.open(settingsUrl) {
print("已打开系统设置自动化页面")
} else {
//
let fallbackUrl = URL(string: "x-apple.systempreferences:com.apple.preference.security")!
NSWorkspace.shared.open(fallbackUrl)
}
}) {
HStack {
Image(systemName: "gearshape.fill")
.frame(width: 20)
.foregroundColor(.blue)
Text("打开系统设置")
.font(.caption)
.foregroundColor(.blue)
}
}
.buttonStyle(.plain)
.padding(4)
//
Button(action: {
let settingsUrl = URL(string: "x-apple.systempreferences:com.apple.preference.usersandgroups?LoginItems")!
NSWorkspace.shared.open(settingsUrl)
}) {
HStack {
Image(systemName: "person.2.fill")
.frame(width: 20)
.foregroundColor(.blue)
Text("管理登录项")
.font(.caption)
.foregroundColor(.blue)
}
}
.buttonStyle(.plain)
.padding(4)
}
.padding(4)
Divider()
// //
Toggle(isOn: Binding( Toggle(isOn: Binding(
get: { appState.isLaunchAtLogin }, get: { appState.isLaunchAtLogin },