swiftui - Xcode content preview - iPhone - Stack Overflow

admin2025-04-29  1

As an absolute beginner I am trying to make an app, and am testing my code in content preview on my Mac. The application in mind will require registration and login as well. While I have no problem with any settings, I am however unable to write "@" in the content preview mobile interface. With the option+Q, it enters a "œ" symbol... When I hit the register button on my app with that symbol in my email it says "badly formatted email" of course. Is there a solution to that? Anyone experienced this?

I tried pasting @ in, but couldn't paste anything from outside content preview. I looked through settings but didn't find any problems.

The relevant part of the code:

struct LoginView: View {
    @State private var email: String = ""
    @State private var password: String = ""
    @State private var isLoggedIn: Bool = false
    @State private var errorMessage: String = ""

    var body: some View {
        VStack(spacing: 20) {
            Text("My Project")
                .font(.largeTitle)
                .fontWeight(.bold)

            TextField("Email", text: $email)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .autocapitalization(.none)
                .keyboardType(.emailAddress)

            SecureField("Password", text: $password)
                .textFieldStyle(RoundedBorderTextFieldStyle())

            if !errorMessage.isEmpty {
                Text(errorMessage)
                    .foregroundColor(.red)
                    .font(.footnote)
            }

            Button(action: {
                loginUser()
            }) {
                Text("Login")
                    .frame(maxWidth: .infinity)
                    .padding()
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(10)
            }

            NavigationLink("Don't have an account? Register here.", value: "Register")
                .font(.footnote)

            NavigationLink(value: "CategorySelection") {
                EmptyView()
            }
            .navigationDestination(for: String.self) { destination in
                if destination == "CategorySelection" {
                    CategorySelectionView()
                } else if destination == "Register" {
                    RegistrationView()
                }
            }
            .disabled(!isLoggedIn)
        }
        .padding()
    }

    private func loginUser() {
        guard !email.isEmpty, !password.isEmpty else {
            errorMessage = "Please enter both email and password."
            return
        }

        Auth.auth().signIn(withEmail: email, password: password) { authResult, error in
            if let error = error {
                errorMessage = error.localizedDescription
            } else {
                errorMessage = ""
                isLoggedIn = true
            }
        }
    }
}

struct RegistrationView: View {
    @State private var email: String = ""
    @State private var password: String = ""
    @State private var errorMessage: String = ""
    @State private var isRegistered: Bool = false

    var body: some View {
        VStack(spacing: 20) {
            Text("Register")
                .font(.largeTitle)
                .fontWeight(.bold)

            TextField("Email", text: $email)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .autocapitalization(.none)
                .keyboardType(.emailAddress)

            SecureField("Password", text: $password)
                .textFieldStyle(RoundedBorderTextFieldStyle())

            if !errorMessage.isEmpty {
                Text(errorMessage)
                    .foregroundColor(.red)
                    .font(.footnote)
            }

            Button(action: {
                registerUser()
            }) {
                Text("Register")
                    .frame(maxWidth: .infinity)
                    .padding()
                    .background(Color.green)
                    .foregroundColor(.white)
                    .cornerRadius(10)
            }

            NavigationLink(value: "Login") {
                EmptyView()
            }
            .navigationDestination(for: String.self) { destination in
                if destination == "Login" {
                    LoginView()
                }
            }
            .disabled(!isRegistered)
        }
        .padding()
    }

    private func registerUser() {
        guard !email.isEmpty, !password.isEmpty else {
            errorMessage = "Please enter both email and password."
            return
        }

        Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
            if let error = error {
                errorMessage = error.localizedDescription
            } else {
                errorMessage = ""
                isRegistered = true // Navigáció a LoginView-re sikeres regisztráció után
            }
        }
    }
}
转载请注明原文地址:http://anycun.com/QandA/1745933568a91318.html