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

SwiftUI中从Safari返回应用主屏幕后更新提示Alert不显示的问题

SwiftUI中从Safari返回应用主屏幕后更新提示Alert不显示的问题

嗨,我来帮你搞定这个问题~

咱们先搞明白为啥从Safari切回来Alert不弹:你现在只在onAppear里调用了appForceUpdate(),但onAppear只有在视图第一次加载、或者从其他页面导航回来的时候才会触发。当你从Safari切回App时,HomeNew视图其实一直处于显示状态,并没有重新“出现”,所以onAppear不会执行,自然也就不会检查更新、弹出Alert了。

那解决思路很简单:除了视图首次出现的时机,咱们还要监听应用从后台回到前台的事件,每次应用激活时都重新检查更新条件。

下面是修改后的完整代码,我标注了新增的关键部分:

import SwiftUI
import UIKit

struct HomeNew: View {

    @StateObject private var loginViewModel = LoginViewModel()
    @State private var showAlert = false
    // 新增:用于监听应用激活通知的publisher
    private let willEnterForegroundPublisher = NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)

    var body: some View {
        ZStack(alignment: .bottomLeading){
            Color.offWhite
                .ignoresSafeArea()
            VStack{
                VStack{
                    HStack(alignment: .center) {
                        VStack(alignment: .leading, spacing: 4) {
                            Text("Welcome")
                                .foregroundColor(.gray)
                            Text(userName)
                                .onTapGesture {
                                    navigationManager.switchToTab(.profile)
                                }
                        }
                        Spacer()
                    }
                    .padding(16)
                }
            }
        }
        .onAppear {
            appForceUpdate()
        }
        // 新增:监听应用从后台回到前台的事件,每次激活都检查更新
        .onReceive(willEnterForegroundPublisher) { _ in
            appForceUpdate()
        }
        .alert("Application Update", isPresented: $showAlert) {
            Button("Update") {
                if let url = URL(string: "https://apps.apple.com/in/app/k12-studio/id6451487315"),
                   UIApplication.shared.canOpenURL(url) {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                }
            }
        } message: {
            Text("New update is available, Please take update!")
        }
    }
    
    private func appForceUpdate() {
        loginViewModel.appVersionUpdate { status in
            if status {
                let criticalVersion = "2.6.9"
                let installedVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "1.1"
                
                print("installedVersion is: \(installedVersion)\ncriticalVersion is: \(criticalVersion)")
                
                let requiresUpdate = installedVersion.compare(criticalVersion, options: .numeric) == .orderedAscending
                
                UserDefaults.standard.set(requiresUpdate, forKey: "RequiresUpdate")
                
                showAlert = UserDefaults.standard.bool(forKey: "RequiresUpdate")
            }
        }
    }
}

关键修改点说明:

  1. 新增了willEnterForegroundPublisher,用来订阅系统的UIApplication.willEnterForegroundNotification通知——这个通知会在应用从后台切回前台时触发。
  2. 用.onReceive绑定这个publisher,每次收到通知就调用appForceUpdate(),重新检查更新条件并设置showAlert的状态。

这样修改后,不管是第一次打开进入Home页,还是从Safari切回App,都会执行更新检查,符合条件的话Alert就会正常弹出来啦~

备注:内容来源于stack exchange,提问作者Swift

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.15 10:47:57