What are the best practices to implement login views for user authentication with SwiftUI Xcode 12? Thank you
Implementing Login with SwiftUI
In Xcode 13, iOS 15, look for
func focused(_ binding: FocusState.Binding, equals value: Value) -> some View where Value : Hashable
in documentation of Xcode:
struct LoginForm {
enum Field: Hashable {
case usernameField
case passwordField
}
@State private var username = ""
@State private var password = ""
@FocusState private var focusedField: Field?
var body: some View {
Form {
TextField("Username", text: $username)
.focused($focusedField, equals: .usernameField)
SecureField("Password", text: $password)
.focused($focusedField, equals: .passwordField)
Button("Sign In") {
if username.isEmpty {
focusedField = .usernameField
} else if password.isEmpty {
focusedField = .passwordField
} else {
handleLogin(username, password)
}
}
}
}
}