SP GlassKit1.1.0

GlassOTPField

One logical one-time-code field with paste, AutoFill and a segmented display.

  • Content
  • iOS 26 · iPadOS 26 · macOS 26
  • Version 1.1.0
  • glass-otp-field

Content on an opaque, readable surface. Glass is kept for controls.

GlassOTPField running in SP Glass Studio.Capture
Capture of the component running in SP Glass Studio on the iOS 26.5 simulator. Liquid Glass is rendered by the system; this is an image, not a live preview.

Installation

spglass add glass-otp-field

Copies the sources and their dependencies into your project. Set up the CLI.

Swift
.package(url: "https://git.spboucher.ai/sp-glasskit.git", from: "1.1.0")

import SPGlassKit
Identifierglass-otp-field
ModuleSPGlassForms
Kindcontent-component — one system TextField under a row of opaque boxes
PlatformsiOS 26, iPadOS 26, macOS 26
ExampleExamples/Gallery/Forms/GlassOTPFieldExample.swift

Minimal usage

Swift
@State private var code = ""

GlassOTPField(code: $code) { code in
    verify(code)
}

Variants

VariantUse
length: 4 … 8Number of digits. Six by default.
hasError: trueOutlines the boxes in the destructive colour after a rejected code.
label:Accessibility label when "Verification code" is not the right name (a PIN, for example).

Parameters

ParameterTypeDefaultMeaning
codeBinding<String>—Digits entered so far, ASCII only.
lengthInt6Number of digits.
labelLocalizedStringKey?"Verification code"Accessibility label.
hasErrorBoolfalseError outline.
onComplete@MainActor (String) -> Void{ _ in }Called each time the code becomes complete.

States

Empty, partially filled (the box that takes the next digit is underlined; while the field has focus it shows a caret and the focus ring), complete (onComplete fires), error (digits and outlines in destructive), disabled. Clearing the binding (code = "") resets the field.

Codes of six or eight digits are drawn as two groups separated by a short dash ("428 – 913"); GlassOTPLayout.groupBoundary(length:) gives the index where the second group starts. The field never shows a placeholder: its label is for assistive technologies only (in 1.0 the label was drawn across the boxes).

Advanced example

Swift
GlassOTPField(code: $code, length: 6, hasError: model.codeRejected) { entered in
    Task { await model.verify(entered) }
}
.onChange(of: code) { _, _ in model.codeRejected = false }
.disabled(model.isVerifying)

Input is cleaned by the pure GlassOTPSanitizer.sanitize(_:length:):

Swift
GlassOTPSanitizer.sanitize("Your code is 482 913.", length: 6)   // "482913"
GlassOTPSanitizer.sanitize("١٢٣٤٥٦", length: 6)                  // "123456"

Accessibility

  • There is one text field, so VoiceOver meets one element: its label and the value "3 of 6 digits entered". The boxes are hidden from assistive technologies and the digits are not read one box at a time by GlassKit.
  • The field uses the one-time-code content type, so the system can offer the code received by message, and the number pad on iOS.
  • Pasting a whole code works as in any text field: long-press or Command-V; separators and letters are dropped.
  • Boxes share the available width up to a square, their height follows Dynamic Type, and the digits shrink rather than clip.
  • The caret of the active box blinks, except with Reduce Motion.
  • Digits are always laid out left to right, including in right-to-left languages.

Limits

  • Digits only; alphanumeric codes are not supported.
  • The code lives only in the host's binding. The component stores, logs and copies nothing; clear the binding when you are done.
  • The text field is drawn transparent over the boxes, so the system caret and selection highlight are not visible; the outlined box shows the position instead.
  • Digits of other scripts are converted to ASCII; symbols that only look like numbers (fractions, Roman numerals, circled digits) are dropped.
  • AutoFill from Messages, the paste menu and VoiceOver output need a device and were not exercised here.

Example

Complete example (compiled with the package)
import SwiftUI
import SPGlassKit

public struct GlassOTPFieldExample: GlassExample {
    public static let componentID = "glass-otp-field"
    /// The demo accepts this fictitious code only.
    private static let expected = "246810"

    @State private var code = ""
    @State private var shortCode = ""
    @State private var status = "Enter the 6-digit demo code 246810."
    @State private var rejected = false

    public init() {}

    public var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 20) {
                GlassOTPField(code: $code, hasError: rejected) { entered in
                    rejected = entered != Self.expected
                    status = rejected ? "That code is not valid. Try again." : "Code accepted."
                }
                .onChange(of: code) { _, newValue in
                    if newValue.count < 6 { rejected = false }
                }

                Label(status, systemImage: rejected ? "xmark.octagon" : "info.circle")
                    .font(.callout)
                    .foregroundStyle(.secondary)

                HStack {
                    Button("Simulate paste “246 810”") { code = GlassOTPSanitizer.sanitize("246 810", length: 6) }
                    Button("Clear") { code = "" }
                }

                Text("Four digits").font(.headline)
                GlassOTPField(code: $shortCode, length: 4, label: "PIN")

                Text("Disabled").font(.headline)
                GlassOTPField(code: .constant("12"), length: 4)
                    .disabled(true)
            }
            .padding()
        }
    }
}

#Preview("GlassOTPField") { GlassOTPFieldExample() }

Registry entry

Identifierglass-otp-field
Version1.1.0
Depends onglass-forms-support, glass-foundation
FrameworksSwiftUI
Public symbolsGlassOTPSanitizer, GlassOTPField, GlassOTPLayout
LicenceMIT
Manifestmanifest.json

Files