0

Hi everyone I'm new to SwiftUI .. I need to present a view containing two textFields with modal presentation only after a condition has been checked.


Example .. When the user pushes the login button I need the app to check the database for the existence of the user. If the user exists he can access the app otherwise he must show a view where he must enter his name and surname.

With UIKit I used this to present a structure or class

self.present(userFound ? Home() : UserNameInfo(), animated: true, completion: nil) 

but with SwiftUI I can't figure out how to solve this problem.

Can you help me in any way?

My code

 var body: some View { ZStack(alignment: .top) { Color(.black).ignoresSafeArea() gradientBackground().ignoresSafeArea() VStack(alignment: .center, spacing: 25) { Spacer() logoStack() // Messaggio di benvenuto VStack(alignment: .leading, spacing: 15) { Text("Effettua l'accesso al tuo account") .font(.title2.bold()) Text("Riserva un momento esclusivo prenotando un taglio tradizionale oppure una rasatura con panni caldi e trattamenti viso") .font(.callout.weight(.light)) } .foregroundColor(.white) //.dynamicTypeSize(.medium) // Apple Sign In Button SignInWithAppleView() .frame(width: screen.size.width - 56) .frame(height: 45, alignment: .center) .onTapGesture(perform: showAppleLoginView) // Divisore Divider().background(.gray) // Messaggio sull'accettazione della Privacy e delle Condizioni di utilizzo VStack(spacing: 5) { Text("Continuando dichiaro di accettare le ") .multilineTextAlignment(.center) HStack(spacing: 0) { Button("Condizioni di Utilizzo") {} .foregroundColor(.white) .font(.footnote.bold()) Text(" ed i ") Button("Termini sulla Privacy") {} .foregroundColor(.white) .font(.footnote.bold()) Text(".") } } .foregroundColor(.gray) .font(.footnote) .padding(.bottom, 25) } .padding(.horizontal) } private func showAppleLoginView() { // Show modalview if user not exist in Firebase } 

// MARK: - Apple Sign In Button View Representable struct SignInWithAppleView: UIViewRepresentable { typealias UIViewType = ASAuthorizationAppleIDButton func makeUIView(context: Context) -> UIViewType { ASAuthorizationAppleIDButton(type: .signIn, style: .white) } func updateUIView(_ uiView: UIViewType, context: Context) {} } 

1 Answer 1

1

You can use the modifier .fullScreenCover

& you just need to pass a binding to a @State var which you set to true when you want to display the modal.

example

struct ExampleScreenView: View { @State var showModal: Bool = false var body: some View { VStack { Text("Some Text") .padding() Button { showModal = true } label: { Text("Show other view") } } .fullScreenCover(isPresented: $showModal) { VStack { Color.white Text("Some other text") .padding() Button { showModal = false } label: { Text("close view") } } } } } 

This example the first view has a button that sets the bool to true, and shows the modal, the modal view has a button that sets the bool to false and closes the view.

The button is just for the sake of an example, but you can use any logic to set the bool. to true, and then you can present whatever view you choose within the fullScreenCover.

Specific for the login scenario, you can use a .onReceive modifier to listen for a notification sent from your login successful code.

.onReceive(NotificationCenter.default.publisher(for: Notification.Name(rawValue: "didLogin")) 

&

NotificationCenter.post(name:Notification.Name("didLogin"), object: nil) 
Sign up to request clarification or add additional context in comments.

4 Comments

Yes I had seen this ... my problem is that the modal view must be presented without using a button because I need the new view to be presented to the user only after considering a condition ... Example .. If the user exists in the database do not present any view, otherwise if the user does not exist in the database I have to show the new view. All this happens only after the user has logged in to Firebase. The check of the user's existence occurs immediately after he has logged into firebase.
I'm edit my post with the current code
Once the condition is met you set the bool to true, that is the purpose of the binding.
If login is elsewhere in the app you could use .onReceive( Notification) to listen for a login notification.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.