I have a OTP screen where user enters his OTP (6-digits) on a text filed (hidden on background) and once user enters the 6th digit, I will dismiss my keyboard and make an API call.
private var maxDigits: Int = 6
@Binding private var pin: String
private var backgroundField: some View {
let boundPin = Binding<String>(get: { self.pin }, set: { newValue in
self.pin = newValue
self.submitPin() // API call method
})
return TextField("", text: boundPin, onCommit: submitPin)
.accentColor(.clear)
.foregroundColor(.clear)
.keyboardType(.numberPad)
.introspectTextField { textField in
textField.tintColor = .clear
textField.textColor = .clear
textField.keyboardType = .numberPad
textField.textContentType = .oneTimeCode
if pin.count == maxDigits {
textField.resignFirstResponder()
} else {
textField.becomeFirstResponder()
}
}
}
The problem I am running into on iOS 15 is, when user enters the 6th digit, the "submitPin" method is getting called 2 times. This is happening only on iOS 15 not on prior OS versions like iOS 14/13/12.
Is this an open bug with Apple (or) what should I need to do to fix this.
Reference Article - https://medium.com/flawless-app-stories/swiftui-passcode-field-for-otp-and-pin-entry-b61ba663dc31