Pre-fill information
On iOS, store previously entered information and reuse this information on different screen. You can use UserDefaults to save the data and retrieve it when needed.
swift
enum UserDefaultKey: String {
static let emailAddress = "emailAddress"
}
class LoginViewController: UIViewController {
@IBOutlet weak var emailTextField: UITextField!
@IBAction func submitButtonTapped(_ sender: UIButton) {
// Store data in UserDefaults on submit
UserDefaults.standard.set(emailTextField.text, forKey: UserDefaultKey.emailAddress)
}
}
class ResetPasswordViewController: UIViewController {
@IBOutlet weak var emailTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Pre-fill saved e-mail address on load
emailTextField.text = UserDefaults.standard.string(forKey: UserDefaultKey.emailAddress)
}
} In SwiftUI, store previously entered information and reuse this information on different screen. You can use UserDefaults to save the data and retrieve it when needed.
swift
enum UserDefaultKey: String {
static let emailAddress = "emailAddress"
}
struct LoginView: View {
// Store `emailAddress` in UserDefaults
@AppStorage(UserDefaultKey.emailAddress)
private var email: String = ""
@State private var password: String = ""
var body: some View {
VStack {
TextField("Email", text: $email)
.keyboardType(.emailAddress)
.textContentType(.emailAddress)
SecureField("Password", text: $password)
Button("Login") {
// Handle login action
}
}
}
}
struct ResetPassword: View {
// Use `emailAddress` from UserDefaults
@AppStorage(serDefaultKey.emailAddress)
private var email: String = ""
var body: some View {
VStack {
TextField("Email", text: $email)
.keyboardType(.emailAddress)
.textContentType(.emailAddress)
Button("Reset password") {
// Handle reset action
}
}
}
}