skip to Main Content

I want an alert to be displayed when the user is moved to a new page without any action and quickly I use NavigationLink

ContentView.swift

struct ContentView: View {
    var body: some View {
        VStack{
            NavigationLink(destination: SecondView()){
                Text("Go to second view")
            }
        }
    }
}

SecondView.swift

struct SecondView: View {
    @State var showAlert = true
    
    var body: some View {
        // i want to show alert when navigate to this view
        VStack{
            Text("Second View")
                .alert(isPresented: $showAlert) {
                    Alert(title: Text("You are in second view"))
                }
        }
    }
}

you can help me ?

2

Answers


  1. Change showAlert value to true when VStack appeared , like that

    struct SecondView: View {
        @State var showAlert = false 
        
        var body: some View {
            // i want to show alert when navigate to this view
            VStack{
                Text("Second View")
                    .alert(isPresented: $showAlert) {
                        Alert(title: Text("You are in second view"))
                    }
            }.onAppear{
              showAlert = true
            }
        }
    }
    
    Login or Signup to reply.
  2. // MARK:- CUSTOM ALERT CLASS
    
    import SwiftUI
    
    private struct AlertView: View {
        
        let loaderBackgroundColor: Color = .secondary
        let loaderCornerRadius: CGFloat =  10.0
        
        /// parameter to hide and show loader
        @Binding var isShowing: Bool
        
        let titleText: String
        let messageText: String
        let buttonText: String
        
        var body: some View {
            
            ZStack(alignment: .center) {
                
                VStack(spacing: 10) {
                    
                    if titleText.count > 0 {
                        Text(titleText)
                            .foregroundColor(.black)
                            .fontWeight(.bold)
                            .padding(.bottom, 10)
                    }
                    
                    Text(messageText)
                        .font(.system(size: 14))
                        .foregroundColor(.gray)
                    
                    Spacer()
                    
                    Button(action: {
                        APAlert.shared.remove()
                    }) {
                        Text(buttonText)
                            .font(.system(size: 14))
                            .foregroundColor(.white)
                            .fontWeight(.bold)
                    }
                    .frame(width: 100, height: 40)
                    .background(Color.black)
                    .cornerRadius(20.0)
                }
                .padding(EdgeInsets(top: 40, leading: 20, bottom: 30, trailing: 20))
                .frame(width: 300, height: 200)
                .background(Color.white)
                .cornerRadius(10.0)
                .shadow(color: Color(.sRGBLinear, white: 0, opacity: 0.13), radius: 10.0)
            }
        }
    }
    
    public class APAlert {
        public static var shared = APAlert()
        private init() { }
        private var popupWindow: AlertWindow?
        
        public func showAlert(title: String = "Error", message: String = "Request could not be processed due to a server error. The request may succeed if you try again.", buttonTitle: String = "Ok") {
            setAlertBody(title: title, message: message, buttonTitle: buttonTitle)
        }
        
        /// function to remove loader from screen.
        public func remove() {
            removeAlert()
        }
        
    }
    
    // MARK: - AlertWindow
    private class AlertWindow: UIWindow {
    }
    
    private extension APAlert {
        
        func setAlertBody(title: String = "", message: String = "", buttonTitle: String = "") {
            
            let windowScene = UIApplication.shared
                .connectedScenes
                .filter { $0.activationState == .foregroundActive }
                .first
            if let windowScene = windowScene as? UIWindowScene {
                popupWindow = AlertWindow(windowScene: windowScene)
                popupWindow?.frame = UIScreen.main.bounds
                popupWindow?.backgroundColor = .clear
                popupWindow?.rootViewController = UIHostingController(rootView: AlertView(isShowing: .constant(true), titleText: title, messageText: message, buttonText: buttonTitle))
                popupWindow?.rootViewController?.view.backgroundColor = UIColor.gray.withAlphaComponent(0.6) //.opacity(0.5)
                popupWindow?.makeKeyAndVisible()
            }
        }
        /// Remove loader from screen
        func removeAlert() {
            let alertwindows = UIApplication.shared.windows.filter { $0 is AlertWindow }
            alertwindows.forEach { (window) in
                window.removeFromSuperview()
            }
            popupWindow = nil
        }
    }
    

    Now here we are going to show alert using the APAlert class

    import SwiftUI
    
    struct ContentView: View {
        
        var body: some View {
            VStack{
                Text("SHOW ALERT").onTapGesture {
                    APAlert.shared.showAlert(title: "Title", message: "Error", buttonTitle: "OK")
                }
            }
        }
    }
    

    Reference :-
    https://github.com/Arvindcs/APAlertView

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search