Title
Has anyone using your Flutter app reported that they tapped the SMS code above the iPhone keyboard and got more digits than were in the message?
On our login screen, tapping 123456 gave me 12345612. Where did that last 12 come from? I logged the value inside Flutter’s native iOS input view and found 123456123456. Our field had an eight-character limit, so only the first two digits of the second copy were visible. In a field without a length limit, you can see the entire code twice.
What happened on one tap?
I reproduced it on an iPhone 17 running iOS 27.2 RC with Flutter 3.35.7. Tapping a real SMS suggestion triggered three callbacks:
-
setMarkedText:selectedRange:wrote123456into the field. -
unmarkTextcommitted it. The field now contained the full code, with the cursor at the end. - UIKit delivered
123456again. That version ofFlutterTextInputViewhad nosetText:method, so UIKit calledinsertText:and appended the code at the cursor. Native trace
What changed in iOS 27.3 beta 1?
I updated the same phone, disabled the workaround, and tried again with the same Flutter engine, input field, and Simplified Chinese keyboard. This time Flutter received a single insertText: call. The field went straight from empty to 123456.
Comparing the two UIKitCore builds, I found a new check for UITextContentTypeOneTimeCode that can skip the earlier write. That matches what I saw on the device. I tested 27.2 RC and 27.3 beta 1, so I can’t say how the final 27.2 or 27.2.1 releases behave. iOS comparison
How to fix it in your Flutter app
The Flutter fix adds setText: to the native input view. When UIKit delivers the full code, Flutter replaces the contents of the field. If the field already contains 123456, replacing it with 123456 leaves exactly six digits. I verified this with a real SMS in a diagnostic build, and the engine fix has been merged into Flutter master. Flutter PR #195149
If you can reproduce the bug and can’t upgrade to an engine containing the fix yet, here’s the full app-side workaround.
1. In Xcode, create FlutterTextInputCompatibility.swift and select your app target under Target Membership (Runner in a standard Flutter project).
import ObjectiveC import UIKit enum FlutterTextInputCompatibility { static func install() { let setter = NSSelectorFromString("setText:") // Skip engines that already provide the setter. guard let inputViewClass: AnyClass = NSClassFromString("FlutterTextInputView"), class_getInstanceMethod(inputViewClass, setter) == nil else { return } let setText: @convention(block) (AnyObject, NSString?) -> Void = { object, text in guard let input = object as? UITextInput, let document = input.textRange( from: input.beginningOfDocument, to: input.endOfDocument ) else { return } // UIKit supplies the complete value, so replace the whole field. input.replace(document, withText: text as String? ?? "") } let implementation = imp_implementationWithBlock(setText) if !class_addMethod(inputViewClass, setter, implementation, "v@:@") { imp_removeBlock(implementation) } } } 2. Open ios/Runner/AppDelegate.swift and add FlutterTextInputCompatibility.install() at the start of your existing didFinishLaunchingWithOptions method. If your app registers plugins there, it looks like this:
override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { FlutterTextInputCompatibility.install() // Add this line. GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } Keep your app’s existing startup code. If you launch a custom FlutterEngine, call install() before the first engine.run().
Rebuild and tap a real SMS suggestion on a device where you saw the bug. The workaround applies to Flutter’s shared native input view, so check password AutoFill too. Once you upgrade to an engine containing the upstream fix and verify it on-device, you can remove the Swift file and the install() call.
Prefer to hand this to your coding assistant?
Paste this into your coding assistant with your Flutter project open:
Check the Flutter engine my iOS app actually builds with. Does its FlutterTextInputView already implement setText:? If it does, show me where and leave the project unchanged. If the setter is missing, add the Swift workaround linked below to my iOS app target. Use the class name from the source you copy. Call its install() method at the start of the existing didFinishLaunchingWithOptions method in AppDelegate.swift. If the app starts a custom FlutterEngine, make sure installation happens before the first engine.run(). Preserve the existing startup and plugin registration code. Build the iOS target, show me the diff and build result, and tell me what to verify on a real iPhone. Swift workaround: https://github.com/flutter/flutter/issues/176872#issuecomment-6362528897 Upstream fix: https://github.com/flutter/flutter/pull/195149 