title: flutter: phone Authentication failed -ERROR: Invalid token.. Try again later
body: <p>Firebase Phone Auth is working in simulator and not working in real device.</p>
  <p>already i created an apn key in developer.apple.com and add that in firebase and enabled background mode and check fetch, process and background.</p>
  <p>This is what the console prints out:</p>
  <pre><code>Runner[11345:2633540] GTMSessionFetcher invoking fetch callbacks, data {length = 252, bytes = 0x7b0a2020 22657272 6f72223a 207b0a20 ... 5d0a2020 7d0a7d0a }, error Error Domain=com.google.HTTPStatus Code=400 &quot;(null)&quot; UserInfo={data={length = 252, bytes = 0x7b0a2020 22657272 6f72223a 207b0a20 ... 5d0a2020 7d0a7d0a }, data_content_type=application/json; charset=UTF-8}
  2021-10-29 22:07:22.273998+0300 Runner[11345:2633546] flutter: Invalid token.
  2021-10-29 22:07:22.523814+0300 Runner[11345:2633540] [connection] nw_resolver_start_query_timer_block_invoke [C3.1] Query fired: did not receive all answers in time for www.googleapis.com:443
  </code></pre>
  <p>My flutter code is like below</p>
  <pre><code>      Future&lt;void&gt; verifyPhone() async {
      await _auth.verifyPhoneNumber(
        phoneNumber: _phoneNumber,
        verificationCompleted: (PhoneAuthCredential credential) async {
          await FirebaseAuth.instance.signInWithCredential(credential);
          final snackBar = SnackBar(content: Text(&quot;Login Success&quot;));
          ScaffoldMessenger.of(context).showSnackBar(snackBar);
        },
        verificationFailed: (FirebaseAuthException e) {
          print(e.message);
          final snackBar =
              SnackBar(content: Text(&quot;verificationFailed ${e.message}&quot;));
          ScaffoldMessenger.of(context).showSnackBar(snackBar);
        },
        codeSent: (String verficationId, int resendToken) {
          setState(() {
            codeSent = true;
            verId = verficationId;
          });
        },
        codeAutoRetrievalTimeout: (String verificationId) {
          setState(() {
            verId = verificationId;
          });
        },
        timeout: Duration(seconds: 60),
      );
    }
  </code></pre>
  <p>AppDelegate.swift</p>
  <pre><code>import UIKit
  import Flutter
  import Firebase
  
  @UIApplicationMain
  @objc class AppDelegate: FlutterAppDelegate {
    override func application(
      _ application: UIApplication,
      didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -&gt; Bool {
      FirebaseApp.configure()
      GeneratedPluginRegistrant.register(with: self)
      return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
  }
  </code></pre>
  <p>Podfile</p>
  <pre><code># Uncomment this line to define a global platform for your project
  # platform :ios, '9.0'
  platform :ios, '10.0'
  # CocoaPods analytics sends network stats synchronously affecting flutter build latency.
  ENV['COCOAPODS_DISABLE_STATS'] = 'true'
  
  project 'Runner', {
    'Debug' =&gt; :debug,
    'Profile' =&gt; :release,
    'Release' =&gt; :release,
  }
  
  def flutter_root
    generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
    unless File.exist?(generated_xcode_build_settings_path)
      raise &quot;#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first&quot;
    end
  
    File.foreach(generated_xcode_build_settings_path) do |line|
      matches = line.match(/FLUTTER_ROOT\=(.*)/)
      return matches[1].strip if matches
    end
    raise &quot;FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get&quot;
  end
  
  require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)
  
  flutter_ios_podfile_setup
  
  target 'Runner' do
    use_frameworks!
    use_modular_headers!
  
    flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
  end
  
  post_install do |installer|
    installer.pods_project.targets.each do |target|
      flutter_additional_ios_build_settings(target)
    end
  end
  </code></pre>
  <p>Info.plist</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;!DOCTYPE plist PUBLIC &quot;-//Apple//DTD PLIST 1.0//EN&quot; &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;
  &lt;plist version=&quot;1.0&quot;&gt;
  &lt;dict&gt;
      &lt;key&gt;CFBundleDevelopmentRegion&lt;/key&gt;
      &lt;string&gt;$(DEVELOPMENT_LANGUAGE)&lt;/string&gt;
      &lt;key&gt;CFBundleDisplayName&lt;/key&gt;
      &lt;string&gt;DOTHawala&lt;/string&gt;
      &lt;key&gt;CFBundleExecutable&lt;/key&gt;
      &lt;string&gt;$(EXECUTABLE_NAME)&lt;/string&gt;
      &lt;key&gt;CFBundleIdentifier&lt;/key&gt;
      &lt;string&gt;$(PRODUCT_BUNDLE_IDENTIFIER)&lt;/string&gt;
      &lt;key&gt;CFBundleInfoDictionaryVersion&lt;/key&gt;
      &lt;string&gt;6.0&lt;/string&gt;
      &lt;key&gt;CFBundleLocalizations&lt;/key&gt;
      &lt;array&gt;
          &lt;string&gt;en&lt;/string&gt;
          &lt;string&gt;fa&lt;/string&gt;
          &lt;string&gt;ar&lt;/string&gt;
      &lt;/array&gt;
      &lt;key&gt;CFBundleName&lt;/key&gt;
      &lt;string&gt;dot_exchange&lt;/string&gt;
      &lt;key&gt;CFBundlePackageType&lt;/key&gt;
      &lt;string&gt;APPL&lt;/string&gt;
      &lt;key&gt;CFBundleShortVersionString&lt;/key&gt;
      &lt;string&gt;$(FLUTTER_BUILD_NAME)&lt;/string&gt;
      &lt;key&gt;CFBundleSignature&lt;/key&gt;
      &lt;string&gt;????&lt;/string&gt;
      &lt;key&gt;CFBundleURLTypes&lt;/key&gt;
      &lt;array&gt;
          &lt;dict&gt;
              &lt;key&gt;CFBundleTypeRole&lt;/key&gt;
              &lt;string&gt;Editor&lt;/string&gt;
              &lt;key&gt;CFBundleURLSchemes&lt;/key&gt;
              &lt;array&gt;
                  &lt;string&gt;com.googleusercontent.apps.768659512914-kgtssn20sbm0ioj47dnp8ekcgsgou7b8&lt;/string&gt;
              &lt;/array&gt;
          &lt;/dict&gt;
      &lt;/array&gt;
      &lt;key&gt;CFBundleVersion&lt;/key&gt;
      &lt;string&gt;$(FLUTTER_BUILD_NUMBER)&lt;/string&gt;
      &lt;key&gt;LSApplicationQueriesSchemes&lt;/key&gt;
      &lt;array&gt;
          &lt;string&gt;https&lt;/string&gt;
          &lt;string&gt;http&lt;/string&gt;
      &lt;/array&gt;
      &lt;key&gt;LSRequiresIPhoneOS&lt;/key&gt;
      &lt;true/&gt;
      &lt;key&gt;NSAppTransportSecurity&lt;/key&gt;
      &lt;dict&gt;
          &lt;key&gt;NSAllowsArbitraryLoads&lt;/key&gt;
          &lt;true/&gt;
      &lt;/dict&gt;
      &lt;key&gt;UIBackgroundModes&lt;/key&gt;
      &lt;array&gt;
          &lt;string&gt;fetch&lt;/string&gt;
          &lt;string&gt;processing&lt;/string&gt;
          &lt;string&gt;remote-notification&lt;/string&gt;
      &lt;/array&gt;
      &lt;key&gt;UILaunchStoryboardName&lt;/key&gt;
      &lt;string&gt;LaunchScreen&lt;/string&gt;
      &lt;key&gt;UIMainStoryboardFile&lt;/key&gt;
      &lt;string&gt;Main&lt;/string&gt;
      &lt;key&gt;UISupportedInterfaceOrientations&lt;/key&gt;
      &lt;array&gt;
          &lt;string&gt;UIInterfaceOrientationPortrait&lt;/string&gt;
          &lt;string&gt;UIInterfaceOrientationLandscapeLeft&lt;/string&gt;
          &lt;string&gt;UIInterfaceOrientationLandscapeRight&lt;/string&gt;
      &lt;/array&gt;
      &lt;key&gt;UISupportedInterfaceOrientations~ipad&lt;/key&gt;
      &lt;array&gt;
          &lt;string&gt;UIInterfaceOrientationPortrait&lt;/string&gt;
          &lt;string&gt;UIInterfaceOrientationPortraitUpsideDown&lt;/string&gt;
          &lt;string&gt;UIInterfaceOrientationLandscapeLeft&lt;/string&gt;
          &lt;string&gt;UIInterfaceOrientationLandscapeRight&lt;/string&gt;
      &lt;/array&gt;
      &lt;key&gt;UIViewControllerBasedStatusBarAppearance&lt;/key&gt;
      &lt;false/&gt;
  &lt;/dict&gt;
  &lt;/plist&gt;
  </code></pre>