示例工程 45-macos-app-lifecycle-bundle
45-macos-app-lifecycle-bundle/Sources/Ch45Kit/Bundle.swift
import Foundation
import SwiftUI
/// `.app` 包的标准结构:内容路径与校验。
public struct BundleLayout: Sendable {
public let executableName: String
public let bundleIdentifier: String
public let displayName: String
public let version: String
public let build: String
public let minimumSystemVersion: String
public init(
executableName: String = "App",
bundleIdentifier: String = "dev.xwrt.tutorial.app",
displayName: String = "示例 App",
version: String = "1.0.0",
build: String = "1",
minimumSystemVersion: String = "27.0"
) {
self.executableName = executableName
self.bundleIdentifier = bundleIdentifier
self.displayName = displayName
self.version = version
self.build = build
self.minimumSystemVersion = minimumSystemVersion
}
public var requiredPaths: [String] {
[
"Contents/MacOS/\(executableName)",
"Contents/Info.plist",
"Contents/Resources",
"Contents/_CodeSignature/CodeResources",
]
}
/// 写入 Info.plist 的键值,覆盖启动所需的最小集合。
public var infoDictionary: [String: Any] {
[
"CFBundleExecutable": executableName,
"CFBundleIdentifier": bundleIdentifier,
"CFBundleName": displayName,
"CFBundleDisplayName": displayName,
"CFBundleShortVersionString": version,
"CFBundleVersion": build,
"CFBundlePackageType": "APPL",
"LSMinimumSystemVersion": minimumSystemVersion,
"NSHighResolutionCapable": true,
]
}
public func plistData() throws -> Data {
try PropertyListSerialization.data(
fromPropertyList: infoDictionary,
format: .xml,
options: 0
)
}
public static func missingPaths(at root: URL, layout: BundleLayout) -> [String] {
layout.requiredPaths.filter {
!FileManager.default.fileExists(atPath: root.appendingPathComponent($0).path)
}
}
}
/// 启动参数:`--key value` 与 `--flag` 的简单解析。
public enum LaunchArguments {
public struct Parsed: Equatable, Sendable {
public var flags: Set<String>
public var values: [String: String]
}
public static func parse(_ arguments: [String]) -> Parsed {
var flags: Set<String> = []
var values: [String: String] = [:]
var index = 0
while index < arguments.count {
let argument = arguments[index]
guard argument.hasPrefix("--") else {
index += 1
continue
}
let key = String(argument.dropFirst(2))
if index + 1 < arguments.count, !arguments[index + 1].hasPrefix("--") {
values[key] = arguments[index + 1]
index += 2
} else {
flags.insert(key)
index += 1
}
}
return Parsed(flags: flags, values: values)
}
}
/// 场景阶段与 App 生命周期的对应。
public enum LifecyclePhase: String, CaseIterable, Sendable {
case launch
case active
case inactive
case background
case terminate
public static func from(scenePhase: String) -> LifecyclePhase? {
switch scenePhase {
case "active": .active
case "inactive": .inactive
case "background": .background
default: nil
}
}
public var allowsAnimation: Bool {
self == .active
}
}
/// 截图:生命周期板。
public struct LifecycleBoard: View {
public init() {}
public var body: some View {
VStack(alignment: .leading, spacing: 10) {
Text("App 生命周期").font(.system(size: 17, weight: .semibold))
ForEach(LifecyclePhase.allCases, id: \.self) { phase in
HStack(spacing: 10) {
Text(phase.rawValue)
.font(.system(size: 11, design: .monospaced))
.frame(width: 90, alignment: .leading)
Text(phase.allowsAnimation ? "可动画 / 可交互" : "降级 / 暂停")
.font(.system(size: 11))
.foregroundStyle(.secondary)
Spacer()
}
}
}
.padding(18)
.frame(width: 340, alignment: .leading)
.background(Color.white)
}
}