import Foundation import SwiftUI /// 沙盒授权项:键与布尔值。签名后写入 entitlements。 public struct Entitlements: Sendable { public var values: [String: Bool] public init(values: [String: Bool] = [:]) { self.values = values } public static func sandboxed( networkClient: Bool = false, userSelectedFiles: Bool = false, downloads: Bool = false ) -> Entitlements { var values: [String: Bool] = [ "com.apple.security.app-sandbox": true ] if networkClient { values["com.apple.security.network.client"] = true } if userSelectedFiles { values["com.apple.security.files.user-selected.read-write"] = true } if downloads { values["com.apple.security.files.downloads.read-write"] = true } return Entitlements(values: values) } public var sandboxEnabled: Bool { values["com.apple.security.app-sandbox"] == true } public var hasNetworkClient: Bool { values["com.apple.security.network.client"] == true } public var hasUserSelectedFiles: Bool { values["com.apple.security.files.user-selected.read-write"] == true } public var hasDownloads: Bool { values["com.apple.security.files.downloads.read-write"] == true } public func problems(requiresFileAccess: Bool) -> [String] { var issues: [String] = [] if !sandboxEnabled { issues.append("未开启 App Sandbox,无法上架 App Store") } if requiresFileAccess && !hasUserSelectedFiles && !hasDownloads { issues.append("需要文件访问但未声明 user-selected 或 downloads") } return issues } public func plistData() throws -> Data { try PropertyListSerialization.data( fromPropertyList: values, format: .xml, options: 0 ) } } /// Info.plist 里的权限用途说明:空或过短会被审核拒绝。 public struct Permission: Hashable, Sendable { public let name: String public let infoKey: String public let purpose: String public init(name: String, infoKey: String, purpose: String) { self.name = name self.infoKey = infoKey self.purpose = purpose } public static let camera = Permission( name: "相机", infoKey: "NSCameraUsageDescription", purpose: "扫描书籍条码,用于快速录入 ISBN" ) public static let microphone = Permission( name: "麦克风", infoKey: "NSMicrophoneUsageDescription", purpose: "录制语音笔记,用于转写成文字" ) public static let files = Permission( name: "文件", infoKey: "NSDocumentsFolderUsageDescription", purpose: "读取用户选择的书籍文件" ) public var isPurposeAcceptable: Bool { purpose.trimmingCharacters(in: .whitespacesAndNewlines).count >= 8 } } /// 安全作用域访问的状态机:开始与停止必须配对。 public struct BookmarkAccess: Equatable, Sendable { public enum State: String, Sendable { case idle case accessing case stopped } public private(set) var state: State = .idle public init() {} public mutating func start(succeeded: Bool = true) -> Bool { guard state != .accessing else { return true } guard succeeded else { return false } state = .accessing return true } public mutating func stop() { guard state == .accessing else { return } state = .stopped } } /// 截图:沙盒与权限板。 public struct SandboxBoard: View { public init() {} private let rows: [(String, String)] = [ ("app-sandbox", "默认拒绝,按需声明"), ("network.client", "出站网络"), ("files.user-selected", "用户选择的文件"), ("files.downloads", "下载目录读写"), ("TCC", "麦克风 / 相机 / 屏幕录制"), ("bookmark", "跨启动访问用户文件"), ] public var body: some View { VStack(alignment: .leading, spacing: 9) { Text("沙盒与授权").font(.system(size: 17, weight: .semibold)) ForEach(rows, id: \.0) { row in HStack(spacing: 10) { Text(row.0) .font(.system(size: 10, design: .monospaced)) .frame(width: 150, alignment: .leading) Text(row.1) .font(.system(size: 11)) .foregroundStyle(.secondary) Spacer() } } } .padding(18) .frame(width: 380, alignment: .leading) .background(Color.white) } }