IPA 升级跳转后等 didEnterBackground 自动 exit(0),让 iOS 顺利替换 IPA

【问题】
当前点"确定升级"后跳 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 <noreply@anthropic.com>
This commit is contained in:
joywayer
2026-06-27 21:11:35 +08:00
co-authored by Claude Opus 4.7
parent 83d9502dee
commit b54803966a
@@ -551,13 +551,33 @@ public final class WebContainerViewController: UIViewController {
private func showIPAUpgradeAlert(downloadURL: String) { private func showIPAUpgradeAlert(downloadURL: String) {
let alert = UIAlertController( let alert = UIAlertController(
title: "需要升级", title: "需要升级",
message: "检测到新版本,请前往下载安装。", message: "检测到新版本,确认后将跳转下载并自动退出当前 App。",
preferredStyle: .alert preferredStyle: .alert
) )
alert.addAction(UIAlertAction(title: "确定", style: .default) { _ in alert.addAction(UIAlertAction(title: "确定", style: .default) { _ in
if let url = URL(string: downloadURL) { guard let url = URL(string: downloadURL) else { return }
UIApplication.shared.open(url) // 用户从 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) present(alert, animated: true)
} }