Skip to main content

How to create secure text fields using SecureField

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to create secure text fields using SecureField 관련

SwiftUI by Example

Back to Home

How to create secure text fields using SecureField | SwiftUI by Example

How to create secure text fields using SecureField

Updated for Xcode 15

SwiftUI's SecureField works almost identically to a regular TextField except the characters are masked out for privacy. Just like TextField, you get to provide a placeholder giving the user a suggestion for what to enter, and the underlying value you bind to is still a plain string so you can check it as needed.

Here's an example that creates a SecureField bound to a local @State property so we can show what they typed:

struct ContentView: View {
    @State private var password: String = ""

    var body: some View {
        VStack {
            SecureField("Enter a password", text: $password)
            Text("You entered: \(password)")
        }
    }
}

Download this as an Xcode projectopen in new window

A text field with a line of 8 dots representing hidden text above the words “You entered: password”.
A text field with a line of 8 dots representing hidden text above the words “You entered: password”.
Similar solutions…
How to customize the submit button for TextField, SecureField, and TextEditor | SwiftUI by Example

How to customize the submit button for TextField, SecureField, and TextEditor
How to format text inside text views | SwiftUI by Example

How to format text inside text views
How to create static labels with a Text view | SwiftUI by Example

How to create static labels with a Text view
How to take action when the user submits a TextField | SwiftUI by Example

How to take action when the user submits a TextField
How to create multi-line editable text with TextEditor | SwiftUI by Example

How to create multi-line editable text with TextEditor

이찬희 (MarkiiimarK)
Never Stop Learning.