import UIKit // 应用委托:承载须在 launch 完成前注册、或经 UIKit 委托回调送达的平台集成——后台续传任务 // 注册(本文件),以及 APNs 远程通知的设备令牌回调(见扩展)。SwiftUI App 经 // @UIApplicationDelegateAdaptor 挂接。 final class AppDelegate: NSObject, UIApplicationDelegate { func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil ) -> Bool { // BGContinuedProcessingTask 的 launch handler 须在 launch 完成前注册。 BackgroundTaskManager.shared.registerOnce() return true } // APNs 注册成功:拿到设备令牌 → 交 PushRegistry 经引擎桥上报后端。 func application( _ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data ) { PushRegistry.shared.didRegister(tokenData: deviceToken) } // APNs 注册失败(模拟器无 APNs 支撑 / 未配能力 / 无网络):退化为仅在线 SSE 收事件,非致命。 func application( _ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error ) { // 仅记录,不打断。真机配齐 Push 能力后这里不会触发。 } // content-available 推送的「短暂后台唤醒」(服务端有消息时触发,见 apns/sender.go):在 ~30s 后台 // 窗口内只做隔离的控件 / 剪贴板会话保活——刷新其自身 access,使关闭 / 墓碑态下的剪贴板保持可用。 // 刻意不触碰引擎主会话(主会话由引擎独占续期;后台刷新它会与挂起引擎的内存令牌失同步而触发重登, // 即 #7)。不维持持续连接:仅此一次短暂处理后即让系统挂起。 func application( _ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void ) { // 被其他设备登出(session:revoked 推送):立即清本地登录态——Keychain 主会话 + 控件会话—— // 并通知前台 UI 回登录页。前台 / 后台 / 关闭态皆据此清:后台关闭态清 Keychain 即下次启动为登出态, // 前台则经 NotificationCenter 即时回登录页。不必等下次请求 401 才发现。 // 与后端 push.KindSessionRevoked 一致(Go 侧常量值)。 if (userInfo["type"] as? String) == "session:revoked" { AuthManager.clearPersistedSession() WidgetSessionStore.clear() NotificationCenter.default.post(name: .cdropSessionRevoked, object: nil) completionHandler(.newData) return } // content-available 后台唤醒:刷新隔离的控件会话(剪贴板保活),不碰引擎主会话(避免 #7)。 Task { await WidgetSessionStore.refreshAccess() completionHandler(.newData) } } } extension Notification.Name { // 被其他设备登出的推送到达:UI 层据此立即回登录页(见 AppRoot.onReceive)。 static let cdropSessionRevoked = Notification.Name("cdrop.sessionRevoked") }