feat(权限): 添加权限检查和管理功能
添加权限检查功能,包括自动启动和系统命令执行权限 在菜单中添加打开系统设置和登录项管理的按钮
This commit is contained in:
@@ -2,6 +2,7 @@ import Foundation
|
||||
import Combine
|
||||
import IOKit.pwr_mgt
|
||||
import ServiceManagement
|
||||
import AppKit
|
||||
|
||||
// MARK: - App State Manager
|
||||
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
|
||||
|
||||
deinit {
|
||||
|
||||
@@ -121,6 +121,57 @@ struct MenuContent: View {
|
||||
|
||||
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(
|
||||
get: { appState.isLaunchAtLogin },
|
||||
|
||||
Reference in New Issue
Block a user