class LoginManager : ObservableObject {
@Published var isLoggedIn = false
func doLogin(username: String, password: String) {
//in here, you'll do your network call
//I've mocked it with a simple async call for now
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
//set this once you get the correct response from your server
//this triggers isActive on the NavigationLink below
self.isLoggedIn = true
}
}
}
struct ContentView : View {
@ObservedObject private var loginManager = LoginManager()
@State var username = ""
@State var password = ""
var body: some View {
NavigationView {
Button(action: {
loginManager.doLogin(username: username, password: password)
}) {
//Login fields...
Text("Log me in")
}.overlay(
NavigationLink(destination: LoggedInView(), isActive: $loginManager.isLoggedIn) {
EmptyView()
}
)
}
}
}
struct LoggedInView : View {
var body: some View {
Text("Logged in")
}
}
references
https://stackoverflow.com/questions/66107887/direct-to-new-view-in-swiftui-after-successful-http-request-login
No comments:
Post a Comment