One-Time Code Field

iOS 16.4+iPadOS 16.4+Not available on macOS

Enters a verification code digit by digit, with AutoFill.

FLXOneTimeCodeField with six boxes and the digits 4829 entered
import SwiftUIstruct OneTimeCodeFieldUsageExample: View {    @State private var code = ""    var body: some View {        FLXOneTimeCodeField("Verification code", text: $code)            .padding(FLXSpacing.xl)    }}

Installation

Requires the design system foundation (tokens and theme).

pnpm dlx shadcn@latest add @swiftui-flx/ios-one-time-code-field

Usage

Apple has no one-time code control, so FLXOneTimeCodeField draws the boxes over a single hidden text field. The keyboard, paste, and AutoFill all come from that system text field.

@State private var code = ""FLXOneTimeCodeField("Verification code", text: $code)

The field accepts digits only. Spaces and dashes in a pasted code are dropped, so 482 913 becomes 482913. Tapping a filled box selects that digit, and the next one you type replaces it. Deleting a digit moves the ones after it back one box.

AutoFill

The field sets .oneTimeCode as its content type, so iOS offers a code above the keyboard as it arrives in Messages, in Mail (iOS 17 and later), or from a password app (iOS 18 and later). No setup is needed.

To restrict a text message code to your website, end the message with a line naming the domain. iOS then offers the code only on that domain or in an app associated with it:

Your Example code is 482913.@example.com #482913

Length

Codes have 6 digits by default. Set length: for other sizes. The field never holds more digits than that.

FLXOneTimeCodeField with four boxes and the digits 73 entered
FLXOneTimeCodeField("PIN", text: $pin, length: 4)

Groups

groupSize splits the boxes into groups with a dash between them, which makes a long code easier to read and compare.

FLXOneTimeCodeField with two groups of three boxes separated by a dash
FLXOneTimeCodeField("Verification code", text: $code)  .flxOneTimeCodeFieldStyle(FLXDefaultOneTimeCodeFieldStyle(groupSize: 3))

Submitting the code

The number pad has no Return key, so there is no .onSubmit. React to a complete code with .onChange(of:), and keep a button for people who'd rather confirm:

Card titled Verify your email with a full code and a Verify button
FLXCard {  FLXCardHeader {      FLXCardTitle("Verify your email")      FLXCardDescription("Enter the code we sent to your email.")  }  FLXCardContent {      FLXOneTimeCodeField("Verification code", text: $code)          .onChange(of: code) { newCode in              if newCode.count == 6 {                  verify(newCode)              }          }  }  FLXCardFooter {      FLXButton(variant: .primary) {          verify(code)      } label: {          Text("Verify").frame(maxWidth: .infinity)      }      .controlSize(.large)      .disabled(code.count < 6)  }}

Clear code after a wrong code, so the next attempt starts from the first box.

Disabled

Disabled FLXOneTimeCodeField at half opacity
FLXOneTimeCodeField("Verification code", text: $code)  .disabled(isVerifying)

Accessibility

  • VoiceOver reads the field as a single element with the title as its label, spells the code one digit at a time, and says how many digits are entered, such as "4 of 6 digits entered."
  • The title isn't shown on screen. Put a visible label or description next to the field, such as "Enter the code we sent to your email."
  • The boxes grow with Dynamic Type until the row fills the available width, then stop.
  • With Reduce Motion on, the cursor in the empty box stays still instead of blinking.

The title is a LocalizedStringResource, so a literal is translated and extracted to your String Catalog. The hint is translated when your String Catalog has the %lld of %lld digits entered. key:

"%lld of %lld digits entered." : {  "localizations" : {    "pt-BR" : { "stringUnit" : { "state" : "translated", "value" : "%1$lld de %2$lld dígitos digitados." } }  }}

Custom styling

FLXOneTimeCodeFieldStyle is an FLX protocol. Override it for a subtree via .flxOneTimeCodeFieldStyle(_:). A tap picks a box by its share of the field's width, so lay the boxes out evenly across the field.

FLXOneTimeCodeField drawn as digits over underlines
struct FLXUnderlineOneTimeCodeFieldStyle: FLXOneTimeCodeFieldStyle {  func makeBody(configuration: Configuration) -> some View {      FLXUnderlineOneTimeCodeFieldStyleBody(configuration: configuration)  }}struct FLXUnderlineOneTimeCodeFieldStyleBody: View {  @Environment(\.flxTheme) private var theme  let configuration: FLXOneTimeCodeFieldStyleConfiguration  var body: some View {      HStack(spacing: FLXSpacing.md) {          ForEach(configuration.slots) { slot in              VStack(spacing: FLXSpacing.sm) {                  (slot.character ?? Text(verbatim: " "))                      .flxFont(.title2)                      .monospacedDigit()                      .foregroundStyle(theme.colors.foreground)                  Rectangle()                      .fill(lineColor(slot))                      .frame(height: FLXBorderWidth.thick)              }              .frame(maxWidth: .infinity)          }      }  }  private func lineColor(_ slot: FLXOneTimeCodeFieldStyleConfiguration.Slot) -> Color {      if slot.isActive || slot.character != nil { return theme.colors.foreground }      return theme.colors.border  }}FLXOneTimeCodeField("Verification code", text: $code)  .flxOneTimeCodeFieldStyle(FLXUnderlineOneTimeCodeFieldStyle())

The style only draws. Typing, paste, AutoFill, and VoiceOver stay with the hidden text field, which the component places over whatever the style returns.

API Reference

FLXOneTimeCodeField

init(    _ titleResource: LocalizedStringResource,    text: Binding<String>,    length: Int = 6)

Also takes a StringProtocol title, used as is.

FLXDefaultOneTimeCodeFieldStyle

init(groupSize: Int? = nil)

FLXOneTimeCodeFieldStyleConfiguration

PropertyTypeDescription
slots[Slot]One per digit of length, in order.
isFocusedBoolWhether the field is being edited.

FLXOneTimeCodeFieldStyleConfiguration.Slot

PropertyTypeDescription
idIntThe slot's position, from 0.
characterText?The digit in the slot, or nil when it's empty.
isActiveBoolWhether the next digit goes into this slot or replaces it.