You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS应用更新弹窗重复弹出问题:如何避免applicationDidBecomeActive重复触发

解决方案:修复iOS应用强制更新弹窗重复堆叠问题

问题核心在于每次应用从后台回到前台时,applicationDidBecomeActive会重复触发延迟更新检查,且没有标记弹窗是否已展示,导致多次弹出。以下是具体修复步骤:


1. 添加弹窗展示状态标记

在ConfigApp类中添加一个标记,记录是否已弹出过强制更新弹窗(可选持久化,避免重启应用后重复触发):

// ConfigApp.swift
static var hasShownForceUpdateAlert: Bool {
    get { UserDefaults.standard.bool(forKey: "HasShownForceUpdateAlert") }
    set { UserDefaults.standard.set(newValue, forKey: "HasShownForceUpdateAlert") }
}

2. 修改更新检查逻辑,避免重复弹窗

在checkUpdate()方法开头先检查标记,已弹出则直接返回;确认要弹窗时先标记状态,再创建弹窗:

// 先判断是否已弹出过弹窗,避免重复触发
guard !ConfigApp.hasShownForceUpdateAlert else { return }

for item in GlobalData.sharedInstance.linkContact?.socialLink?.forceUpdate ?? [] {
    if item == currentVersionDouble {
        // 标记弹窗已展示
        ConfigApp.hasShownForceUpdateAlert = true
        
        let alert = UIAlertController(title: "Thông báo", message: "Chúng tôi đã phát hành một phiên bản mới hơn cho ứng dụng này. Bạn có muốn cập nhật không?", preferredStyle: .alert)
        let actionUpgrade = UIAlertAction(title: "Cập nhật", style: .default) { (_: UIAlertAction) in
            if let requestUrlStore = URL(string: urlLink.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "") {
                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(requestUrlStore, options: [:], completionHandler: nil)
                } else {
                    UIApplication.shared.openURL(requestUrlStore)
                }
            }
        }
        alert.addAction(actionUpgrade)
        
        // 检查当前是否已有弹窗展示,避免堆叠
        if let topVC = UIApplication.getTopViewController(), topVC.presentedViewController == nil {
            topVC.present(alert, animated: true, completion: nil)
        }
        break
    }
}

3. 优化延迟任务,防止重复触发

在AppDelegate中管理延迟任务,每次进入活跃状态时取消之前的任务,避免多次触发:

// AppDelegate.swift
private var updateCheckWorkItem: DispatchWorkItem?

func applicationDidBecomeActive(_ application: UIApplication) {
    // 取消之前未执行的延迟任务
    updateCheckWorkItem?.cancel()
    
    let workItem = DispatchWorkItem {
        ConfigApp.checkUpdate()
    }
    updateCheckWorkItem = workItem
    
    DispatchQueue.main.asyncAfter(deadline: .now() + 5, execute: workItem)
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // 进入后台时取消任务,避免后台执行
    updateCheckWorkItem?.cancel()
}

4. 版本更新时重置标记

当用户更新应用版本后,需要重置弹窗标记,保证新版本能正常触发检查:

// 在AppDelegate的didFinishLaunchingWithOptions中添加
if let currentVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
    let savedVersion = UserDefaults.standard.string(forKey: "LastAppVersion") ?? ""
    if currentVersion != savedVersion {
        ConfigApp.hasShownForceUpdateAlert = false
        UserDefaults.standard.set(currentVersion, forKey: "LastAppVersion")
    }
}

内容的提问来源于stack exchange,提问作者Tuấn Anh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 12:23:10