<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: zheng li</title>
    <description>The latest articles on DEV Community by zheng li (@leazer).</description>
    <link>https://dev.to/leazer</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4282429%2F06b041ee-2526-48f5-9421-d7362d393f97.png</url>
      <title>DEV Community: zheng li</title>
      <link>https://dev.to/leazer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/leazer"/>
    <language>en</language>
    <item>
      <title>Why does iOS SMS AutoFill add extra digits to a verification code in Flutter?</title>
      <dc:creator>zheng li</dc:creator>
      <pubDate>Fri, 04 Dec 2026 09:49:16 +0000</pubDate>
      <link>https://dev.to/leazer/why-does-ios-sms-autofill-add-extra-digits-to-a-verification-code-in-flutter-4k4b</link>
      <guid>https://dev.to/leazer/why-does-ios-sms-autofill-add-extra-digits-to-a-verification-code-in-flutter-4k4b</guid>
      <description>&lt;p&gt;&lt;strong&gt;Title&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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?&lt;/p&gt;

&lt;p&gt;On our login screen, tapping &lt;code&gt;123456&lt;/code&gt; gave me &lt;code&gt;12345612&lt;/code&gt;. Where did that last &lt;code&gt;12&lt;/code&gt; come from? I logged the value inside Flutter’s native iOS input view and found &lt;strong&gt;&lt;code&gt;123456123456&lt;/code&gt;&lt;/strong&gt;. 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.&lt;/p&gt;

&lt;h3&gt;
  
  
  What happened on one tap?
&lt;/h3&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;setMarkedText:selectedRange:&lt;/code&gt; wrote &lt;code&gt;123456&lt;/code&gt; into the field.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;unmarkText&lt;/code&gt; committed it. The field now contained the full code, with the cursor at the end.&lt;/li&gt;
&lt;li&gt;UIKit delivered &lt;code&gt;123456&lt;/code&gt; again. That version of &lt;code&gt;FlutterTextInputView&lt;/code&gt; had no &lt;code&gt;setText:&lt;/code&gt; method, so UIKit called &lt;code&gt;insertText:&lt;/code&gt; and appended the code at the cursor. &lt;a href="https://github.com/flutter/flutter/issues/176872#issuecomment-6362339880" rel="noopener noreferrer"&gt;Native trace&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What changed in iOS 27.3 beta 1?
&lt;/h3&gt;

&lt;p&gt;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 &lt;code&gt;insertText:&lt;/code&gt; call. The field went straight from empty to &lt;code&gt;123456&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Comparing the two UIKitCore builds, I found a new check for &lt;code&gt;UITextContentTypeOneTimeCode&lt;/code&gt; 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. &lt;a href="https://github.com/flutter/flutter/issues/176872#issuecomment-7152046593" rel="noopener noreferrer"&gt;iOS comparison&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How to fix it in your Flutter app
&lt;/h3&gt;

&lt;p&gt;The Flutter fix adds &lt;code&gt;setText:&lt;/code&gt; to the native input view. When UIKit delivers the full code, Flutter replaces the contents of the field. If the field already contains &lt;code&gt;123456&lt;/code&gt;, replacing it with &lt;code&gt;123456&lt;/code&gt; leaves exactly six digits. I verified this with a real SMS in a diagnostic build, and the engine fix has been merged into Flutter &lt;code&gt;master&lt;/code&gt;. &lt;a href="https://github.com/flutter/flutter/pull/195149" rel="noopener noreferrer"&gt;Flutter PR #195149&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you can reproduce the bug and can’t upgrade to an engine containing the fix yet, here’s the full app-side workaround.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. In Xcode, create &lt;code&gt;FlutterTextInputCompatibility.swift&lt;/code&gt; and select your app target under Target Membership&lt;/strong&gt; (&lt;code&gt;Runner&lt;/code&gt; in a standard Flutter project).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;ObjectiveC&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;UIKit&lt;/span&gt;

&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="nc"&gt;FlutterTextInputCompatibility&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;install&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;setter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;NSSelectorFromString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"setText:"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;// Skip engines that already provide the setter.&lt;/span&gt;
        &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;inputViewClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AnyClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;NSClassFromString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"FlutterTextInputView"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="nf"&gt;class_getInstanceMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inputViewClass&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nd"&gt;@convention&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;AnyObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;NSString&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Void&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
            &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;object&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;UITextInput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;textRange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                      &lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;beginningOfDocument&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                      &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;endOfDocument&lt;/span&gt;
                  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="c1"&gt;// UIKit supplies the complete value, so replace the whole field.&lt;/span&gt;
            &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;withText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;implementation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;imp_implementationWithBlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;class_addMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inputViewClass&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;implementation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"v@:@"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;imp_removeBlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;implementation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Open &lt;code&gt;ios/Runner/AppDelegate.swift&lt;/code&gt; and add &lt;code&gt;FlutterTextInputCompatibility.install()&lt;/code&gt; at the start of your existing &lt;code&gt;didFinishLaunchingWithOptions&lt;/code&gt; method.&lt;/strong&gt; If your app registers plugins there, it looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;override&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UIApplication&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;didFinishLaunchingWithOptions&lt;/span&gt; &lt;span class="n"&gt;launchOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;UIApplication&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LaunchOptionsKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;FlutterTextInputCompatibility&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;install&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// Add this line.&lt;/span&gt;
    &lt;span class="nc"&gt;GeneratedPluginRegistrant&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;didFinishLaunchingWithOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;launchOptions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep your app’s existing startup code. If you launch a custom &lt;code&gt;FlutterEngine&lt;/code&gt;, call &lt;code&gt;install()&lt;/code&gt; before the first &lt;code&gt;engine.run()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;install()&lt;/code&gt; call.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prefer to hand this to your coding assistant?
&lt;/h3&gt;

&lt;p&gt;Paste this into your coding assistant with your Flutter project open:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>debugging</category>
      <category>flutter</category>
      <category>ios</category>
      <category>mobile</category>
    </item>
  </channel>
</rss>
