0

Hi Is it possible to get textContentType raw value in swift/swiftui without user having to tap on it when keyboard appears.

I want the value to get copied in the background state without getting user to click on it so it autofills textfields.

struct ContentView: View {
    
    @State var code: String = ""
    @State var pasteBoard = UIPasteboard.general
    
    var body: some View {
        VStack {
            TextField("Code", text: $code)
                .textContentType(.oneTimeCode)
                .keyboardType(.numberPad)
                .onChange(of: code) { newValue in
                    pasteBoard.string = newValue
                }
                .padding()
        }
    }
}

I am using pasteboard to copy any newly inserted value into the textfield.

Like in the image below I am using .textContentType(.oneTimeCode) to get an otp code from an sms message, I want this to however be send to the textfield without user interaction, is that possible?

enter image description here

devdchaudhary
  • 467
  • 2
  • 13
  • check this: https://stackoverflow.com/questions/71153222/how-to-auto-fill-otp-textfield-on-otp-code-received-from-firebase-in-ios-swift – ChrisR Feb 15 '23 at 21:43
  • aah okay thanks alot! sucks there's no way to get otp without autofill UI though – devdchaudhary Feb 16 '23 at 19:20

0 Answers0