From b54803966a26e0089839e959bfc0f97165ac831f Mon Sep 17 00:00:00 2001 From: joywayer Date: Sat, 27 Jun 2026 21:11:35 +0800 Subject: [PATCH] =?UTF-8?q?IPA=20=E5=8D=87=E7=BA=A7=E8=B7=B3=E8=BD=AC?= =?UTF-8?q?=E5=90=8E=E7=AD=89=20didEnterBackground=20=E8=87=AA=E5=8A=A8=20?= =?UTF-8?q?exit(0)=EF=BC=8C=E8=AE=A9=20iOS=20=E9=A1=BA=E5=88=A9=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=20IPA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 【问题】 当前点"确定升级"后跳 Safari,但旧版本 App 仍在前台/后台 active —— iOS 安装 新 IPA 时会卡住或要求用户手动 kill 旧 App,体验不顺。 【方案】 跳 Safari 后挂一次性 didEnterBackground 监听 → 0.3s 后 exit(0): - 等系统完成 active → inactive → background 切换再 exit,不会被 Springboard 视为 crash - 比直接 exit(0) 优雅,避免前台闪退动画 - 用户从 Safari 取消回来不退(下次切后台才触发) 【UX 同步】 alert message 提示"确认后将跳转下载并自动退出当前 App",让用户预期对齐。 不影响 H5 契约(纯原生外壳交互)。 Co-Authored-By: Claude Opus 4.7 --- .../WebView/WebContainerViewController.swift | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/ylgamehall/Source/WebView/WebContainerViewController.swift b/ylgamehall/Source/WebView/WebContainerViewController.swift index c6c760a..866e443 100644 --- a/ylgamehall/Source/WebView/WebContainerViewController.swift +++ b/ylgamehall/Source/WebView/WebContainerViewController.swift @@ -551,13 +551,33 @@ public final class WebContainerViewController: UIViewController { private func showIPAUpgradeAlert(downloadURL: String) { let alert = UIAlertController( title: "需要升级", - message: "检测到新版本,请前往下载安装。", + message: "检测到新版本,确认后将跳转下载并自动退出当前 App。", preferredStyle: .alert ) alert.addAction(UIAlertAction(title: "确定", style: .default) { _ in - if let url = URL(string: downloadURL) { - UIApplication.shared.open(url) + guard let url = URL(string: downloadURL) else { return } + // 用户从 Safari 点"安装"完成 IPA 替换时,iOS 要求旧版本 App 不在运行 + // —— 若旧 App 仍在前/后台 active,"安装"按钮会卡住或要求用户手动 kill。 + // + // 解法:跳 Safari 后等 App 真正 didEnterBackground 再 exit(0) + // - 等系统进入后台 = iOS 已完成 active → inactive → background 切换,再 exit 不会被视为 crash + // - 比直接 exit(0) 优雅:避免前台用户看到"闪退"动画 / Springboard 记 crash + // - 一次性 observer,触发后立即 remove;用户从 Safari 取消回来不退(下次切后台才退) + var observerHandle: NSObjectProtocol? + observerHandle = NotificationCenter.default.addObserver( + forName: UIApplication.didEnterBackgroundNotification, + object: nil, + queue: .main + ) { _ in + if let h = observerHandle { + NotificationCenter.default.removeObserver(h) + } + // 给 iOS 0.3s 完成"进入后台"动画与状态保存,避免 SIGTERM 太快引起异常报告 + DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { + exit(0) + } } + UIApplication.shared.open(url) }) present(alert, animated: true) }