Swiftui: Navigate to Home Screen After Login Completed. Navigating Views by Button Click

swiftUI: Navigate to Home screen after Login Completed. Navigating Views by Button click

There is an issue with some View ATM (hope it will be fixed soon).

In order to use code posted, you will need to write something like:

struct ContentView: View {

@EnvironmentObject var userAuth: UserAuth

@ViewBuilder
var body: some View {
if !userAuth.isLoggedin {
return LoginView()
} else {
return NextView()
}
}
}

At the list, at the moment of writing, this was the only thing working for me - both for body and for Group.

Reference for future: date 24 Oct 2019.

Programmatically navigate to new view in SwiftUI

You can replace the next view with your login view after a successful login. For example:

struct LoginView: View {
var body: some View {
...
}
}

struct NextView: View {
var body: some View {
...
}
}

// Your starting view
struct ContentView: View {

@EnvironmentObject var userAuth: UserAuth

var body: some View {
if !userAuth.isLoggedin {
LoginView()
} else {
NextView()
}

}
}

You should handle your login process in your data model and use bindings such as @EnvironmentObject to pass isLoggedin to your view.

Note: In Xcode Version 11.0 beta 4, to conform to protocol 'BindableObject' the willChange property has to be added

import Combine

class UserAuth: ObservableObject {

let didChange = PassthroughSubject<UserAuth,Never>()

// required to conform to protocol 'ObservableObject'
let willChange = PassthroughSubject<UserAuth,Never>()

func login() {
// login request... on success:
self.isLoggedin = true
}

var isLoggedin = false {
didSet {
didChange.send(self)
}

// willSet {
// willChange.send(self)
// }
}
}

Navigate to new screen on button click SwiftUI

Here is possible approach

1) Add link tag state variable to your View

@State private var current: Int? = nil

2) Wrap your view hierarchy into NavigationView to make possible NavigationLink to work

3) Add tagged NavigationLink above your button

NavigationLink(destination: YourDestinationViewHere(), tag: 1, selection: $current) {
EmptyView()
}

4) Add link tag selection to button action

Button(action: {
if self.userName.count <= 5 {
self.isAlertShown = true
} else {
self.current = 1 // this activates NavigationLink with specified tag
}
})

SwiftUI -- Button to go to Home Screen in app

I have the same button as you but It's hard to make it. Maybe the best option is creating a coordinator class, which responsability is take care of all the bindings of your app. Or even better, with an eviroment variable you can controll things easier...

However, I'm not doing like this.. I found this answer, and what I'm doing is passing the binding for all screens (in my case make sense).

Navigate after successful login with SwiftUI

If you don't want to deal with NavigationLink, as @Paulw11 mentioned in the comments, you can conditionally display a logged in/logged out view based on a property you set on an ObservableObject. You can even add animations/transitions. See below for a simple example:


class LoginManager : ObservableObject {
@Published var isLoggedIn = false

func login() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
withAnimation {
self.isLoggedIn = true
}
}
}
}

struct ContentView : View {
@StateObject var loginManager = LoginManager()

var body: some View {
if loginManager.isLoggedIn {
LoggedInView()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.transition(.move(edge: .leading))
} else {
LoginView(loginManager: loginManager)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.transition(.move(edge: .leading))
}
}
}

struct LoginView : View {
@ObservedObject var loginManager : LoginManager

var body: some View {
Button("Login") {
loginManager.login()
}
}
}

struct LoggedInView : View {
var body: some View {
Text("Logged in!")
}
}

I might also consider, if I were you, using at authStateListener and setting the logged in property based on that. That way, if the app is re-opened and the user is still logged in, they'll be transitioned to the logged-in page automatically.

How do I navigate to another screen from a login screen with SwiftUI

If you need to present a view, you can look at this article

On the contrary if the concern is not to show the back button, on the home view one can hide the back button using the configuration like

.navigationBarBackButtonHidden(true)



Related Topics



Leave a reply



Submit