swiftswiftuibuttonsafarialert

Alert not showing after back from Safari to Home Screen in SwiftUI


I am showing Alert message to update my app to latest version so in alert Update button i am opening my Appstore app link in safari page. here when i click update button Appstore link opens in safari and without updating my app i just go back to home then alert has to show but it is not showing why? how to show alert when i back to home from safari? please guide.

Note: only if i back from safari alert not showing but if i navigate to other screen to home again then alert showing but not from safari back

struct HomeNew: View {

@StateObject private var loginViewModel = LoginViewModel()
@State private var showAlert = false

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()
    }
    .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")
        }
    }
}
}

o/p:

installedVersion is: 2.6.7 criticalVersion is: 2.6.9


Solution

  • When you back from Safari, onAppear is not called so there is your update alert is not showing ( because HomeView isn’t really “disappearing” when you open Safari, case for HomeView disappearing is like when you navigate to a new screen, therefore no dissappearing so no reappearing).

    You should observe to ScenePhase to know when your app becomes active again and show update alert.

    struct HomeNew: View {
        @Environment(\.scenePhase) var scenePhase
        
        var body: some View {
            ...
                .onAppear {
                    appForceUpdate()
                }
                .onChange(of: scenePhase) { newPhase in
                    if newPhase == .active {
                        appForceUpdate()
                    }
                }
        }
    }